Introduction
This article mainly talks about the life cycle of beans in the Spring IOC container
Spring bean life cycle
The bean declaration cycle in Spring can be divided into the following four stages:
- Instantiation phase--Instantiation calls the constructor
- Attribute assignment phase-Populate sets up dependency injection
- Initialization --- Initialization call Init method
- Destruction-Destruction calls the Destory method
Interfaces and methods at various stages
Each stage is a specific interface and method. According to the type of method and interface, we can be divided into the following categories:
- The interface and method of bean itself init method, destroy method
- Bean life cycle interface BeanNameAware, ApplicationContextAware, BeanFactoryAware, InitializingBean, DisposableBean
- The container-level life cycle interface mainly includes BeanPostProcessor, InstantiationAwareBeanPostProcessor
- Factory-level processing interface BeanFactoryPostProcessor
Implementation process
Just mentioned the order of execution of many interfaces and methods above?
BeanLifecycle code is as follows
/**
* @ClassName BeanLifecycle
* @Auther burgxun
* @Description: Spring bean 的生命周期
* @Date 2020/4/21 12:45
**/
public class BeanLifecycle implements BeanNameAware, ApplicationContextAware, BeanFactoryAware,InitializingBean,
DisposableBean {
public BeanLifecycle() {
System.out.println("========>【Bean】【BeanLifecycle】执行 构造函数");
}
@Override
public void setBeanName(String s) {
System.out.println("========>【Bean】【BeanNameAware】 执行方法 setBeanName -------》实例化bean后 " +
"为bean 注入BeanName-" + s);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("========>【Bean】【ApplicationContextAware】 执行方法 " +
"setApplicationContext-------》实例化bean后 为bean注入ApplicationContext");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("========>【Bean】【BeanFactoryAware】 执行方法 setBeanFactory -------》实例化bean后 ");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("========>【Bean】【InitializingBean】执行方法 afterPropertiesSet -------》bean" +
" 实例化完成后 初始化之前调用");
}
public void beanInit() {
System.out.println("========>【Bean】【BeanLifecycle】 执行方法 Init-Method " +
"-------》xml中配置bean实例化完成后 初始化方法");
}
public void beanDestroy() {
System.out.println("========>【Bean】【BeanLifecycle】 执行方法 Destroy-Method " +
"-------》xml中配置销毁bean之前 回调方法" +
" ");
}
public void sayHello() {
System.out.println("========>【Bean】【BeanLifecycle】执行方法 bean中方法 -------》 sayHello");
}
@Override
public void destroy() throws Exception {
System.out.println("========>【Bean】【DisposableBean】 执行方法 destroy -------》 销毁 Bean 的回调方法");
}
}
The ContainerLifecycle method is as follows:
**
* @ClassName ContainerLifecycle
* @Auther burgxun
* @Description: Spring 容器 生命周期
* @Date 2020/4/21 14:44
**/
@Component
public class ContainerLifecycle extends InstantiationAwareBeanPostProcessorAdapter {
public ContainerLifecycle() {
System.out.println("========>【Container】【ContainerLifecycle】 执行 构造函数");
}
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
System.out.println("========>【Container】【InstantiationAwareBeanPostProcessor】执行 " +
"postProcessBeforeInstantiation" +
"-------》Bean实例化之前调用 beanName:" + beanName);
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
System.out.println("========>【Container】【InstantiationAwareBeanPostProcessor】执行 " +
"postProcessAfterInstantiation " +
"-------》Bean实例化之后调用 beanName:" + beanName);
return super.postProcessAfterInstantiation(bean, beanName);
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("========>【Container】【BeanPostProcessor】 执行" +
"postProcessBeforeInitialization " +
"-------》Bean初始化之前调用 beanName:" + beanName);
return super.postProcessBeforeInitialization(bean, beanName);
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("========>【Container】【BeanPostProcessor】执行 " +
"postProcessAfterInitialization " +
"-------》Bean初始化之后调用 beanName:" + beanName);
return super.postProcessAfterInitialization(bean, beanName);
}
@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
System.out.println("========>【Container】【InstantiationAwareBeanPostProcessor】 执行 postProcessProperties " +
"Bean 属性赋值的时候 beanName:" + beanName);
return null;
}
}
FactoryLifecycle code is as follows
/**
* @ClassName FactoryLifecycle
* @Auther burgxun
* @Description: Spring beanFactory 如下
* @Date 2020/4/21 17:32
**/
public class FactoryLifecycle implements BeanFactoryPostProcessor {
public FactoryLifecycle() {
System.out.println("========>【BeanFactory】【BeanFactoryPostProcessor】 执行 FactoryLifecycle " +
"构造函数");
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
System.out.println("========>【BeanFactory】【BeanFactoryPostProcessor】 执行方法 " +
"postProcessBeanFactory ");
}
}
Spring bean Test
public class SpringTest {
@Test
public void mySpringBeanTest() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath" +
":Spring-life.xml");
BeanLifecycle beanLifecycle = (BeanLifecycle) context.getBean("beanLifecycle");
beanLifecycle.sayHello();
context.close();
}
}
The execution result is:
【BeanFactory】【BeanFactoryPostProcessor】 执行 FactoryLifecycle 构造函数
【BeanFactory】【BeanFactoryPostProcessor】 执行方法 postProcessBeanFactory
【Container】【ContainerLifecycle】 执行 构造函数
【Container】【InstantiationAwareBeanPostProcessor】执行 postProcessBeforeInstantiation-------》Bean实例化之前调用 beanName:beanLifecycle
【Bean】【BeanLifecycle】执行 构造函数
【Container】【InstantiationAwareBeanPostProcessor】执行 postProcessAfterInstantiation -------》Bean实例化之后调用 beanName:beanLifecycle
【Container】【InstantiationAwareBeanPostProcessor】 执行 postProcessProperties Bean 属性赋值的时候 beanName:beanLifecycle
【Bean】【BeanNameAware】 执行方法 setBeanName -------》实例化bean后 为bean 注入BeanName-beanLifecycle
【Bean】【BeanFactoryAware】 执行方法 setBeanFactory -------》实例化bean后 为bean注入BeanFactory
【Bean】【ApplicationContextAware】 执行方法 setApplicationContext-------》实例化bean后 为bean注入ApplicationContext
【Container】【BeanPostProcessor】 执行postProcessBeforeInitialization -------》Bean初始化之前调用 beanName:beanLifecycle
【Bean】【InitializingBean】执行方法 afterPropertiesSet -------》bean 实例化完成后 初始化之前调用
【Bean】【BeanLifecycle】 执行方法 Init-Method -------》xml中配置bean实例化完成后 初始化方法
【Container】【BeanPostProcessor】执行 postProcessAfterInitialization -------》Bean初始化之后调用 beanName:beanLifecycle
【Bean】【BeanLifecycle】执行方法 bean中方法 -------》 sayHello
【Bean】【DisposableBean】 执行方法 destroy -------》 销毁 Bean 的回调方法
【Bean】【BeanLifecycle】 执行方法 Destroy-Method -------》xml中配置销毁bean之前 回调方法
Then continue to draw a flowchart
The process of the method has been sorted out
Summarize again
the three calling methods of initializing and destroying beans in Spring
- Completed through the InitializingBean / DisposableBean interface
- Specify the method to call by adding init-method / destroy-method to the bean element configuration
- Specify the calling method via @PostConstruct or @PreDestroy annotation
What is their order of execution?
The answer is the annotation "interface method" method configured in the bean element