Preface: Recently, I was asked about the difference between BeanFactory and FactoryBean in an interview. Although this is a very basic Spring knowledge point, I seldom pay attention to it as a business development, so I came back to make some summaries.

BeanFactory

BeanFactory is an interface that provides the most basic form of the IOC container and provides the specification for the implementation of a specific IOC container. It is a primitive Factory in Spring. In Spring, all beans are managed by the BeanFactory, so the BeanFactory stands for IOC container or object factory. At the same time, the Spring on the BeanFactory did a lot of expansion, have different implementations, such as DefaultListableBeanFactory, XmlBeanFactory, ApplicationContext, etc.

As you can see from the above definition, BeanFactory is a Spring container for managing beans. It can have different implementation classes, but all have the same specification.

FactoryBean

Definition of a FactoryBean: A FactoryBean is an interface that you can implement to customize the logic of the Bean to be instantiated. Common factoryBeans are ProxyFactoryBean :Aop proxy Bean GsonFactoryBean:Gson

Conclusion: From the above definition, BeanFactory and FactoryBean are not closely related. The role of BeanFactory is to act as a specification for container implementation, while FactoryBean is concerned with Bean creation process. Developers can customize Bean instantiation through FactoryBean.

Reference: juejin. Cn/post / 684490… Juejin. Cn/post / 684490… Blog.csdn.net/saienenen/a…