Spring bean 生命周期管理

 Spring bean 生命周期管理


 



 For a bean to get into a usable state after instantiation, it needs to
perform some initialization. Likewise, some clean up may be necessary when
the bean is no longer required and is removed from the container.
 当我们要准备使用一个bean时,它必须得执行一些初始化先。相反,当容器关闭的时候,它也可能想要一些清理工作,其实把这两种行为当作c++中的构造函数和析构函数好了。


 Spring provides us with callback methods for the life cycle of the bean. You can have a method in your bean that runs when the bean has been created, and you can also have a method in your bean that is run when the bean is about to be destroyed.
 spring通过回调方法来管理bean的生命周期。


 Spring's BeanFactory manages the life cycle of beans created through the Spring IoC container. The life cycle of beans consist of callback methods, which can be categorized broadly into the following two groups:


• Post-initialization callback methods
• Pre-destruction callback methods
主要分为两类回调方法:
Post-initialization 和Pre-destruction。
The following figure illustrates the two groups:

Spring bean 生命周期管理_第1张图片








Initialization

It represents a sequence of activities that take place between the bean instantiation and the handover of its reference to the client application:


• The bean container finds the definition of the Spring bean in the
configuration file and creates an instance of the bean
• If any properties are mentioned, populate the properties using setters
• If the Bean class implements the BeanNameAware interface, then call the
setBeanName() method
• If the Bean class implements the BeanFactoryAware interface, then call
the setBeanFactory() method
• If the Bean class implements the ApplicationContextAware interface,
then call the setApplicationContext() method
• If there are any BeanPostProcessors objects associated with the
BeanFactory interface that loaded the bean, then Spring will call the
postProcessBeforeInitialization() method before the properties
for the bean are injected
• If the Bean class implements the InitializingBean interface, then call the
afterPropertiesSet() method once all the bean properties defined in the
configuration file are injected
• If the bean definition in the configuration file contains the init-method
attribute, then call this method after resolving the value for the attribute
to a method name in the Bean class
• The postProcessAfterInitialization() method will be called if there
are any bean post processors attached to the BeanFactory interface that
loads the bean。


Activation

The bean has been initialized and the dependency has been injected. Now the bean is ready to be used by the application.


Destruction

This represents the following sequence of activities:
• If the Bean class implements the DisposableBean interface, then call the
destroy() method when the application no longer needs the bean reference
• If the bean definition in the configuration file contains the destroy-method
attribute, then call this method after resolving the value for the attribute to a method name in the Bean class.




There are two important bean life cycle callback methods that are required at the time of bean initialization and its destruction.
• Initialization callbacks
• Destruction callbacks


Initialization callbacks

There are three ways in which you can achieve the initialization work after all necessary properties on the bean are set by the container:
• Implementing the org.springframework.beans.factory.InitializingBean interface
• Using init-method in the XML configuration
• 利用PostConstruct注解


Destruction callbacks

There are two ways you can do a destruction callback:
• Implementing the org.springframework.beans.factory.DisposableBean
interface
• Using destroy-method in the XML configuration
• 利用PreDestroy注解

你可能感兴趣的:(spring,bean,生命周期管理)