The difference between BeanFactory and FactoryBean in spring

Difference: BeanFactory is a Factory, that is, IOC container or object factory, and FactoryBean is a Bean. In Spring, all beans are managed by the BeanFactory (that is, the IOC container). But for FactoryBean, this bean is not a simple bean, but a factory bean that can produce or modify objects. Its implementation is similar to the factory pattern and decorator pattern in the design pattern.


1.  BeanFactory

    BeanFactory defines the most basic form of the IOC container, and provides the most basic interface that the IOC container should comply with, that is , the lowest level and the most basic programming specification that Spring IOC complies with. In Spring code, BeanFactory is just an interface, not a specific implementation of the IOC container, but the Spring container provides many implementations, such as  DefaultListableBeanFactory , XmlBeanFactory , ApplicationContext , etc., all of which are implementations with additional functions.

Java code
  1. package  org.springframework.beans.factory;  
  2. import  org.springframework.beans.BeansException;  
  3. public  interface  BeanFactory {  
  4.     String FACTORY_BEAN_PREFIX =  "&" ;  
  5.     Object getBean(String name)  throws  BeansException;  
  6.     <T> T getBean(String name, Class<T> requiredType)  throws  BeansException;  
  7.     <T> T getBean(Class<T> requiredType)  throws  BeansException;  
  8.     Object getBean(String name, Object... args)  throws  BeansException;  
  9.     boolean  containsBean(String name);  
  10.     boolean  isSingleton(String name)  throws  NoSuchBeanDefinitionException;  
  11.     boolean  isPrototype(String name)  throws  NoSuchBeanDefinitionException;  
  12.     boolean  isTypeMatch(String name, Class<?> targetType)  throws  NoSuchBeanDefinitionException;  
  13.     Class<?> getType(String name)  throws  NoSuchBeanDefinitionException;  
  14.     String[] getAliases(String name);  
  15. }   
 

2. FactoryBean In
general, Spring uses the class attribute of <bean> to specify the implementation class to instantiate beans through the reflection mechanism. In some cases, the process of instantiating beans is more complicated. A lot of configuration information is provided in . The flexibility of the configuration method is limited, and a simple solution may be obtained by using the encoding method. Spring provides a factory class interface of org.springframework.bean.factory.FactoryBean for this purpose, and users can customize the logic of instantiating beans by implementing this interface.
The FactoryBean interface occupies an important position for the Spring framework, and Spring itself provides more than 70 FactoryBean implementations. They hide the details of instantiating some complex beans and bring convenience to upper-layer applications. Since Spring 3.0, FactoryBean began to support generics, that is, the interface declaration was changed to the form of FactoryBean<T>

Java code
  1. package  org.springframework.beans.factory;  
  2. public  interface  FactoryBean<T> {  
  3.     T getObject()  throws  Exception;  
  4.     Class<?> getObjectType();  
  5.     boolean  isSingleton();  
  6. }   
 The following three methods are also defined in this interface:
TgetObject(): Returns the Bean instance created by FactoryBean. If isSingleton() returns true, the instance will be placed in the single-instance cache pool in the Spring container;
booleanisSingleton(): Returns whether the scope of the Bean instance created by FactoryBean is singleton or prototype;
Class<T>getObjectType(): Returns the Bean type created by FactoryBean.
When the implementation class configured by the class attribute of <bean> in the configuration file is FactoryBean, what is returned by the getBean() method is not the FactoryBean itself, but the object returned by the FactoryBean#getObject() method, which is equivalent to the FactoryBean#getObject() proxy the getBean() method.
Example: If the <bean> of Car below is configured in the traditional way, each property of Car corresponds to a <property> element tag.

Java code
  1. package   com.baobaotao.factorybean;  
  2.     public    class   Car {  
  3.         private    int  maxSpeed ​​;  
  4.         private   String brand ;  
  5.         private    double  price ;  
  6.         public    int   getMaxSpeed() {  
  7.             return    this  .maxSpeed ​​;  
  8.         }  
  9.         public    void   setMaxSpeed(  int   maxSpeed ​​) {  
  10.             this.maxSpeed  ​​= maxSpeed;  
  11.         }  
  12.         public   String getBrand() {  
  13.             return    this.brand  ;  
  14.         }  
  15.         public    void   setBrand( String brand ) {  
  16.             this.brand  = brand;  
  17.         }  
  18.         public    double   getPrice() {  
  19.             return    this  . price ;  
  20.         }  
  21.         public    void   setPrice(  double   price ) {  
  22.             this.price  = price;  
  23.        }  
  24. }   
     If it is implemented in the way of FactoryBean , it will be more flexible. The following example specifies configuration values ​​for all properties of Car at one time by means of comma separators :

Java code
  1. package   com.baobaotao.factorybean;  
  2. import   org.springframework.beans.factory.FactoryBean;  
  3. public    class   CarFactoryBean   implements   FactoryBean<Car> {  
  4.     private   String carInfo ;  
  5.     public   Car getObject ()    throws   Exception {  
  6.         Car car =   new   Car () ;  
  7.         String[] infos = carInfo.split(  ","  );  
  8.         car.setBrand(infos[  0  ]);  
  9.         car.setMaxSpeed(Integer.valueOf(infos[  1  ]));  
  10.         car.setPrice( Double.valueOf( infos[  2  ]) ) ;  
  11.         return   car;  
  12.     }  
  13.     public   Class<Car> getObjectType () {  
  14.         return   Car.class   ;   _
  15.     }  
  16.     public    boolean   isSingleton() {  
  17.         return    false  ;  
  18.     }  
  19.     public   String getCarInfo () {  
  20.         return    this  .carInfo ;  
  21.     }  
  22.   
  23.     // Accept comma separator to set attribute information  
  24.     public    void   setCarInfo( String carInfo ) {  
  25.         this.carInfo  = carInfo;  
  26.     }  
  27. }   
 With this CarFactoryBean, you can use the following custom configuration method to configure CarBean in the configuration file:
<beanid="car"class="com.baobaotao.factorybean.CarFactoryBean"
P:carInfo="Ferrari,400 ,2000000"/>
When calling getBean("car"), Spring finds that CarFactoryBean implements the interface of FactoryBean through the reflection mechanism, and then the Spring container calls the interface method CarFactoryBean#getObject() method to return. If you want to get an instance of CarFactoryBean, you need to add a "&" prefix before the beanName when using the getBean(beanName) method: such as getBean("&car");

3. The difference between
BeanFactory is a Factory, that is, IOC container or object factory, and FactoryBean is a Bean. In Spring, all beans are managed by the BeanFactory (that is, the IOC container). But for FactoryBean, this bean is not a simple bean, but a factory bean that can produce or modify objects. Its implementation is similar to the factory pattern and decorator pattern in the design pattern.

Tags: The difference between BeanFactory and FactoryBean in spring

spring parsing

Related: The difference between BeanFactory and FactoryBean in spring