The difference and connection between BeanFactory and FactoryBean

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, which is the lowest and most basic programming specification that Spring IOC complies with. In the 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., which are all implementations with additional functions.

package org.springframework.beans.factory;  
import org.springframework.beans.BeansException;  
public interface BeanFactory {  
    String FACTORY_BEAN_PREFIX = "&";  
    Object getBean(String name) throws BeansException;  
    <T> T getBean(String name, Class<T> requiredType) throws BeansException;  
    <T> T getBean(Class<T> requiredType) throws BeansException;  
    Object getBean(String name, Object... args) throws BeansException;  
    boolean containsBean(String name);  
    boolean isSingleton(String name) throws NoSuchBeanDefinitionException;  
    boolean isPrototype(String name) throws NoSuchBeanDefinitionException;  
    boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException;  
    Class<?> getType(String name) throws NoSuchBeanDefinitionException;  
    String[] getAliases(String name);  
}     

2. FactoryBean In
general, Spring uses the class attribute of the reflection mechanism to specify the implementation class to instantiate the Bean. In some cases, the process of instantiating the Bean is more complicated. If you follow the traditional way, you need to provide a lot of configuration information. . The flexibility of the configuration method is limited. In this case, a simple scheme may be obtained by using the coding method. Spring aims to provide a plant org.springframework.bean.factory.FactoryBean class interface, the user can instantiate the Bean by implementing this interface customization logic Series.
The FactoryBean interface occupies an important position for the Spring framework. Spring itself provides more than 70 FactoryBean implementations. They hide the details of instantiating some complex Beans and bring convenience to upper-level applications. Starting from Spring 3.0, FactoryBean began to support generics, that is, the interface declaration was changed to the form of FactoryBean:

package org.springframework.beans.factory;  
public interface FactoryBean<T> {  
    T getObject() throws Exception;  
    Class<?> getObjectType();  
    boolean isSingleton();  
} 

The following 3 methods are also defined in this interface:

T getObject() : Returns the Bean instance created by FactoryBean. If isSingleton() returns true, the instance will be placed in the single-instance buffer pool in the Spring container;

boolean isSingleton() : Returns whether the scope of the Bean instance created by FactoryBean is singleton or prototype;

Class getObjectType() : Returns the Bean type created by FactoryBean.

When the implementation class configured by the class attribute 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 FactoryBean#getObject() proxying getBean( ) Method.

Example: If the following Car is configured in the traditional way, each attribute of Car corresponds to an element tag.

package  com.baobaotao.factorybean;  
    public   class  Car  {  
        private   int maxSpeed ;  
        private  String brand ;  
        private   double price ;  
        public   int  getMaxSpeed ()   {  
            return   this . maxSpeed ;  
        }  
        public   void  setMaxSpeed ( int  maxSpeed )   {  
            this . maxSpeed  = maxSpeed;  
        }  
        public  String getBrand ()   {  
            return   this . brand ;  
        }  
        public   void  setBrand ( String brand )   {  
            this . brand  = brand;  
        }  
        public   double  getPrice ()   {  
            return   this . price ;  
        }  
        public   void  setPrice ( double  price )   {  
            this . price  = price;  
       }  
}   

If you use FactoryBean to achieve flexibility, the following example uses a comma separator to specify configuration values ​​for all attributes of Car at one time:

package  com.baobaotao.factorybean;  
import  org.springframework.beans.factory.FactoryBean;  
public   class  CarFactoryBean  implements  FactoryBean<Car>  {  
    private  String carInfo ;  
    public  Car getObject ()   throws  Exception  {  
        Car car =  new  Car () ;  
        String []  infos =  carInfo .split ( "," ) ;  
        car.setBrand ( infos [ 0 ]) ;  
        car.setMaxSpeed ( Integer. valueOf ( infos [ 1 ])) ;  
        car.setPrice ( Double. valueOf ( infos [ 2 ])) ;  
        return  car;  
    }  
    public  Class<Car> getObjectType ()   {  
        return  Car. class ;  
    }  
    public   boolean  isSingleton ()   {  
        return   false ;  
    }  
    public  String getCarInfo ()   {  
        return   this . carInfo ;  
    }  
  
    // 接受逗号分割符设置属性信息  
    public   void  setCarInfo ( String carInfo )   {  
        this . carInfo  = carInfo;  
    }  
}   

With this CarFactoryBean, you can configure the Car Bean in the configuration file using the following custom configuration method:

<bean id=“car” class=“com.baobaotao.factorybean.CarFactoryBean”

P:carInfo="法拉利 ,400,2000000"/>

When getBean("car") is called, Spring finds that CarFactoryBean implements the FactoryBean interface through the reflection mechanism, and then the Spring container calls the interface method CarFactoryBean#getObject() to return. If you want to get an instance of CarFactoryBean, you need to add the "&" prefix to the display before beanName when using the getBean(beanName) method: such as getBean("&car");

3. Difference

BeanFactory is a Factory, that is, IOC container or object factory, 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.

Guess you like

Origin blog.csdn.net/u011582840/article/details/107883280