This article GitHub location: https://github.com/WillVi/Spring-Annotation/
Previous article: Spring Annotation Development-comprehensive analysis of common annotations using the component Register Bean lifecycle
? Create------> Initialize-------> Destroy. The container manages the bean's lifecycle uniformly, and we can also customize the initialization and destruction methods, and the container calls our custom initialization and destruction methods when the bean is in the current life cycle.
Process:
-
Construction (object creation)
- Single instance: Create an object when the container is started
- Multi-instance: Create an object at each fetch
-
Beanpostprocessor.postprocessbeforeinitialization
? PS: Traverse to get all the beanpostprocessor in the container, execute beforeinitialization, one but return null, jump out of the For loop, No subsequent beanpostprocessor.postprocessorsbeforeinitialization will be executed.
-
Initialize: Object creation is complete, and assignment is good, call initialization method ...
-
Beanpostprocessor.postprocessafterinitialization
-
Destroyed:
- Single instance: When the container is closed
- Multi-instance: The container does not manage the bean, and the container does not call the Destroy method;
Beanpostprocessor principle
The following method executes in turn:
populateBean(beanName, mbd, instanceWrapper);Assigning attributes to a bean
? Initializebean
{
`applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);`初始化前执行`invokeInitMethods(beanName, wrappedBean, mbd);`执行自定义初始化`applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);`初始化后执行
}
The use of beanpostprocessor at the bottom of spring
1. bean赋值2. 注入其他组件3. @Autowired4. 生命周期注解功能5. @Async,xxx
Use:
-
Specify initialization and destruction methods, specify Initmethod and Destroymethod by @bean;
Cases:@Bean(initMethod="init",destroyMethod="destroy")
-
Disposablebean (Defining the destruction logic) by having the bean implement Initializingbean (define initialization logic)
package cn.willvi.bean;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;import org.springframework.stereotype.Component;@Componentpublic class Man implements InitializingBean,DisposableBean{ public Man() { System.out.println("Man创建"); } public void destroy() { System.out.println("Man摧毁"); } public void afterPropertiesSet() throws Exception { System.out.println("Man初始化"); }}
- The
-
can use JSR250;
- @PostConstruct: The Bean is created and the property assignment is complete; To execute the initialization method
- @ Predestroy: Notifies us of cleanup work before the container destroys the bean
package Cn.willvi.bean;import Javax.annotation.postconstruct;import Javax.annotation.predestroy;import org.springframework.stereotype.Component; @Componentpublic class Woman {public Woman () {System.out.println ("Woman create" ); } @PreDestroy public void Destroy () {System.out.println ("Execute Before Woman Destroy"), @PostConstruct public void init () {Syste M.OUT.PRINTLN ("Woman Initialization"); }}
?
-
Beanpostprocessor "Interface": The post processor of the bean, and some processing before and after the bean initialization
- Postprocessbeforeinitialization: Working before initialization
- Postprocessafterinitialization: Working after initialization
package cn.willvi.bean;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.stereotype.Component;@Componentpublic class ManBeanPostProcessor implements BeanPostProcessor{ public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException { if(arg1.equals("man")) { System.out.println("----------------------BeanPostProcessor初始化后执行"); } return arg0; } public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { if(arg1.equals("man")) { System.out.println("---------------------BeanPostProcessor初始化前"); } return arg0; }}
Spring Annotation Development-comprehensive analysis of the life cycle of common annotation usage methods