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
Spring
In , IOC
is a very important concept, its essence is the map structure, storage container and business bean information. However BeanFactory
, FactoryBean
the difference between and is a very important knowledge point. In this article, the source code will be analyzed and explained.
difference and connection
BeanFactory
Spring
In , all Bean
are managed by BeanFactory
(that is, the IOC container). BeanFactory
The 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
, XmlBeanFactory
and interfaces that add additional functions. BeanFactory
The created Bean
object needs to follow the following life cycle form, and Bean
the production is realized through the reflection mechanism.
In English, it Aware
means consciousness. You can see that BeanFactory
the life cycle process includes many Aware
interfaces:
The following are the following methods related to the BeanFactory
interface 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 Bean
instantiation 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 FactoryBean
needs to construct Bean
objects without following Bean
the flow of the life cycle. Spring
It provides many implementations FactoryBean
by , which hide a series of complex details of instantiation and bring convenience to upper-layer applications. Since Srping3.0 FactoryBean
began to support generics, that is, the interface declaration is changed to FactoryBean<T>
the form of . In FactoryBean
the application of , SqlSessionFactoryBean
it is a good practice. In the process of operating the database, it provides SqlSession
a Factory
.
Summarize
BeanFactory
Provides a Spring IOC container specification that can produce and manage a factory interface for beans. But it FactoryBean
is 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.