The difference between Spring BeanFactory and FactoryBean

Get into the habit of writing together! This is the 4th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

The public shooting falcon is above the high tower, and there is no disadvantage in getting it.

foreword

SpringIn , IOCis a very important concept, its essence is the map structure, storage container and business bean information. However BeanFactory, FactoryBeanthe difference between and is a very important knowledge point. In this article, the source code will be analyzed and explained.

difference and connection

BeanFactory

SpringIn , all Beanare managed by BeanFactory(that is, the IOC container). BeanFactoryThe basic form of the container is defined, and the basic interface and life cycle of the IOC container are specified. There are also many implementation classes for BeanFactory, such as ApplicationContext, DefaultListableBeanFactory, XmlBeanFactoryand interfaces that add additional functions. BeanFactoryThe created Beanobject needs to follow the following life cycle form, and Beanthe production is realized through the reflection mechanism.

In English, it Awaremeans consciousness. You can see that BeanFactorythe life cycle process includes many Awareinterfaces:

Implementation of BeanFactory

The following are the following methods related to the BeanFactoryinterface life cycle, which are widely used in project development, ApplicationContextAware, andInitializingBean .DisposableBean

BeanNameAware.setBeanName 用于设置 Bean 的名称
BeanClassLoaderAware.setBeanClassLoader 设置类加载器
BeanFactoryAware.setBeanFactory 设置 bean 工厂
ResourceLoaderAware.setResourceLoader 设置资源加载器
ApplicationEventPublisherAware.setApplicationEventPublisher 设置事件发布器
MessageSourceAware.setMessageSource 设置信息资源
ApplicationContextAware.setApplicationContext 设置应用上下文
ServletContextAware.setServletContext 设置 Servlet 上下文
BeanPostProcessor.postProcessBeforeInitialization 前置处理器
InitializingBean.afterPropertiesSet Bean 初始化操作
RootBeanDefinition.getInitMethodName 设置Bean 的初始化方法名称
BeanPostProcessor.postProcessAfterInitialization 后置处理器
DisposableBean.destroy 设置 Bean 销毁
RootBeanDefinition.getDestroyMethodName 获取 Bean 销毁的方法
复制代码

By default, if you directly call the getBean method, it will return a factory-created object. If you want to get the Bean itself, you need to add the & symbol as a prefix for processing.

FactoryBean

However, in some specific cases, the Beaninstantiation operation will be very complicated, and a large number of properties need to be configured according to its requirements. At this time ,Bean the configuration flexibility of FactoryBeanneeds to construct Beanobjects without following Beanthe flow of the life cycle. SpringIt provides many implementations FactoryBeanby , which hide a series of complex details of instantiation and bring convenience to upper-layer applications. Since Srping3.0 FactoryBeanbegan to support generics, that is, the interface declaration is changed to FactoryBean<T>the form of . In FactoryBeanthe application of , SqlSessionFactoryBeanit is a good practice. In the process of operating the database, it provides SqlSessiona Factory.

Summarize

BeanFactoryProvides a Spring IOC container specification that can produce and manage a factory interface for beans. But it FactoryBeanis a special way of bean creation, which does not need to follow the specification of the IOC container, but an extension to the bean. For complex bean object creation and use it can encapsulate the creation details of the object.

Guess you like

Origin juejin.im/post/7082620724443086855