Difference between BeanFactory and FactoryBean

1.BeanFactory

             BeanFactory is the most basic container of IOC, responsible for producing and managing beans. It provides the most basic specifications for other specific IOC containers, such as DefaultListableBeanFactory, XmlBeanFactory, ApplicationContext and other specific containers that implement BeanFactory, and then build on it. Additional functions are added.

BeanFactory source code:

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);  
}  

 It can be seen that BeanFactory is an interface, which mainly contains some overloaded methods for obtaining beans.

 

2.FactoryBean

              FactoryBean is an interface. When the Bean in the IOC container implements FactoryBean, the Bean object obtained through getBean(String beanName) is not the implementation class object of FactoryBean, but the object returned by the getObject() method in the implementation class. . To get the implementation class of FactoryBean, getBean(&BeanName), add & before beanName.

 FactoryBean source code:

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

 

3. The difference between BeanFactory and FactoryBean

              There is actually no comparison between BeanFactory and FactoryBean, but the names of the two are very close, so sometimes they are compared. BeanFactory provides the most basic form of IOC container and provides specifications for the implementation of specific IOC containers. FactoryBean can be said to provide a more flexible way for the implementation of beans in the IOC container. FactoryBean adds a simple factory mode and decoration mode to the implementation of beans on the basis of the IOC container. We can configure it flexibly in the getObject() method. In fact, there are many FactoryBean implementation classes in the Spring source code. If you want to understand FactoryBean deeply and accurately, you can only read the Spring source code.

 

Reference: https://blog.csdn.net/wangbiao007/article/details/53183764

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326065273&siteId=291194637