The utilization of Spring2.0 technique Factorybean

Source: Internet
Author: User
Tags aop

There are two types of beans in spring, one is a normal bean and the other is a factory bean, or Factorybean. A factory bean differs from a normal bean in that the object returned is not an instance of the specified class, and it returns the object returned by the factory Bean's GetObject method. Within the spring framework, AOP-related functionality and transactional processing, where factory beans are used in many places, this article simply analyzes the usage of factory beans.
First of all, look at the ordinary bean, the normal bean directly returns an instance of the specified class (the value of the class attribute in the configuration), as the following Bean1 returns a string string:

Below, take a look at a simple factory bean:
Import Java.util.Date;
Import Org.springframework.beans.factory.FactoryBean;
public class Factorybeandemo implements Factorybean {
private String name;
public void SetName (String name)
Public Object GetObject () throws Exception {
if ("Date". Equals (name)) return new date ();
else return new string ("This is a string!");
Public Class Getobjecttype () {
Return "date". Equals (name)? Date.class:String.class;
public Boolean Issingleton () {
return false;

Here's a look at the factory bean configuration:

Here is the test code:
Resource res=new Classpathresource ("Springroad/deomo/chap4/demo-bean.xml");
Beanfactory factory=new xmlbeanfactory (res);
System.out.println (Factory.getbean ("Myfactorybean"). GetClass ());
System.out.println (Factory.getbean ("MyFactoryBean1"). GetClass ());

Output Result:
Class Java.lang.String
Class Java.util.Date

Thus, by using Factorybean, we can get different types of object instances. This is how we can return different types of business objects in AOP by setting CALSS to Proxyfactorybean. If you can use Factorybean flexibly in practical applications, you can add a lot of magical features to your application.

A little trick:
Factorybean also hides a trick, which is what to do when we actually need to return to the Factorybean instance. That is, we need to get a factorybean through the container in the application. At this point, you need to use a special way, that is, to precede the bean's name with a special symbol "&".
For example, the following code:
System.out.println (Factory.getbean ("&myfactorybean"). GetClass ());
System.out.println (Factory.getbean ("&myfactorybean1"). GetClass ());
The result of the output will be:
Class Springroad.deomo.chap4.FactoryBeanDemo
Class Springroad.deomo.chap4.FactoryBeanDemo

Due to the limited level, the lack of the text welcomes the broad masses of spring enthusiasts feel free, please "Spring fan" do not gather!

(Note: The author, easyjf Open source Team Big Gorge, reproduced please keep the author's statement.) )


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.