The difference and function of BeanFactory and FactoryBean

Preface

This question is often asked in Spring interviews, so this article will sort it out!

Difference, effect

BeanFactory:

  1. BeanFactory is also a factory. Beans in Spring are all produced and managed by BeanFactory, except for special Beans, such as those implemented by FactoryBean!
  2. BeanFactory is the core interface that implements the IOC container, and its responsibilities include instantiating, locating, configuring objects in the application and establishing dependencies between these objects.
  3. BeanFactory is an interface that provides the most basic form of IOC container, which is to provide Spring implementation of IOC container specifications, not the specific implementation of IOC container, specific implementations such as DefaultListableBeanFactory, XmlBeanFactory, ApplicationContext, of which XmlBeanFactory is the commonly used one, this implementation The objects that make up the application and the dependencies between the objects will be described in XML. The XmlBeanFactory class will hold this XML configuration metadata and use it to build a fully configurable system or application. The realization relationship is as shown in the figure below!
    Insert picture description here
    Insert picture description here
  4. BeanFacotry is a relatively primitive Factory in spring. For example, XMLBeanFactory is a typical BeanFactory.
    The original BeanFactory cannot support many spring plug-ins, such as AOP functions, web applications, etc. The ApplicationContext interface, which is derived from the BeanFactory interface, ApplicationContext contains all the functions of the BeanFactory, and it is usually recommended to take precedence over the BeanFactory

FactoryBean:

FactoryBean is an interface and a Bean. This Bean is not a simple Bean, but a factory Bean that can produce or modify objects. The implementation of FactoryBean is similar to the factory mode and modifier mode in the design mode. In general, Spring passes The reflection mechanism uses the class attribute of the bean in the xml 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 in the bean in the xml. The flexibility of the configuration method is limited. It provides a more flexible way for the IOC container to implement Beans. Using the coding method may result in a simple solution, that is, instead of following the BeanFactory process specification, FactoryBean gives Beans on the basis of the IOC container. The realization of adding a simple engineering mode and a costumer mode, we only need to rewrite the getObject method after implementing the FactoryBean interface, and return our custom Bean! The details of instantiating some complex Beans are hidden, which brings 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, where the return value of the isSingleton method determines whether the object returned by the current implementation of the FactoryBean is stored in the singleton pool, which is the first level cache!

@Component
public class MyFactoryBean implements FactoryBean {
    
    

    @Override
    public Object getObject() throws Exception {
    
    
        A testMain = new A();
        System.out.println("----------一系列而外的定制操作-----------");
        //以直接通过getObject方法来完成定制化的Bran,无需使用BeanFactory一步一步按流程生成!
        return testMain;
    }

    @Override
    public Class<?> getObjectType() {
    
    
        return A.class;
    }

    @Override
    public boolean isSingleton() {
    
    
        return true;
    }
}

Guess you like

Origin blog.csdn.net/CSDN877425287/article/details/114096631