Bean生命周期
bean生命周期主要是4个阶段:创建、注入、初始化、销毁。
通过 BeanPostProcessor等处理器扩展功能
本例中用到的处理器有:
- postProcessBeforeDestruction
- postProcessBeforeInstantiation、postProcessAfterInstantiation
- postProcessProperties
package com.example.demo.springTest.a03;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.example.demo.springTest.a03")
@Slf4j
public class A03Application {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(A03Application.class, args);
String marker = "mmmmmmmm";
log.info(marker +"01 已启动");
LifeCycleBean bean = run.getBean(LifeCycleBean.class);
log.info(marker +"02 获取lifecycleBean "+bean);
bean.destroy2();
log.info(marker +"03 结束");
run.close();
}
}
package com.example.demo.springTest.a03;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Slf4j
@Component
public class LifeCycleBean {
public LifeCycleBean(){
log.info("========"+"LifeCycleBean 构造");
}
@Autowired
public void autowire(@Value("${JAVA_HOME}") String home){
log.info("========"+"LifeCycleBean 装配" +home);
}
@PostConstruct
public void init2(){
log.info("========"+"LifeCycleBean 初始化");
}
@PreDestroy
public void destroy2(){
log.info("========"+"LifeCycleBean 销毁");
}
}
package com.example.demo.springTest.a03;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class MyBeanPostProcessor implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor {
@Override
public void postProcessBeforeDestruction(Object o, String s) throws BeansException {
log.info("<<<<<<<< "+s+" : 销毁前执行");
if (s.equals("lifeCycleBean")){
log.info("<<<<<<<< 2销毁前执行,如@PreDestroy");
}
}
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
if (beanName.equals("lifeCycleBean")){
log.info("22222222"+"创建_前");
}
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
if (beanName.equals("lifeCycleBean")){
log.info("33333333"+"创建_后");
}
return true;
}
@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
if (beanName.equals("lifeCycleBean")){
log.info("44444444"+"依赖注入阶段。eg:@Autowired @Value @Resource");
}
return null;
}
}
输出验证
##构造前、后,有增强的处理postProcessBeforeInstantiation、postProcessAfterInstantiation
2023-06-26 23:41:51.776 [main] INFO [com.example.demo.springTest.a03.MyBeanPostProcessor:24] - 22222222创建_前
2023-06-26 23:41:51.776 [main] INFO [com.example.demo.springTest.a03.LifeCycleBean:17] - ========LifeCycleBean 构造
2023-06-26 23:41:51.776 [main] INFO [com.example.demo.springTest.a03.MyBeanPostProcessor:32] - 33333333创建_后
##注入前,有增强的处理 postProcessProperties
2023-06-26 23:41:51.776 [main] INFO [com.example.demo.springTest.a03.MyBeanPostProcessor:40] - 44444444依赖注入阶段。eg:@Autowired @Value @Resource
2023-06-26 23:41:51.776 [main] INFO [com.example.demo.springTest.a03.LifeCycleBean:23] - ========LifeCycleBean 装配C:\Java\jdk1.8.0_341
2023-06-26 23:41:51.776 [main] INFO [com.example.demo.springTest.a03.LifeCycleBean:28] - ========LifeCycleBean 初始化
##销毁前,有增强的处理 postProcessBeforeDestruction
2023-06-26 23:41:54.755 [main] INFO [com.example.demo.springTest.a03.MyBeanPostProcessor:17] - <<<<<<<< 2销毁前执行,如@PreDestroy
2023-06-26 23:41:54.755 [main] INFO [com.example.demo.springTest.a03.LifeCycleBean:33] - ========LifeCycleBean 销毁