涉及的点:BeanPostProcessor接口(bean后处理器)、InstantiationAwareBeanPostProcessor接口(实例化对象后处理器)、DestructionAwareBeanPostProcessor接口(销毁bean后处理器)、InitializingBean接口、@PostConstruct、@PreDestroy
注意:在bean对象的创建过程中,处理的一直是类实例对象,等到完整的bean创建流程完成之后,这个对象才叫做bean对象
所谓的BeanPostProcessor其实是Spring为我们打开的一个窗口,就犹如拦截器一样,在bean对象初始化之前和初始化完成之后,可以对bean进行进一步的包装;BeanPostProcessor的作用就是对Spring工厂所创建的对象,进行再加工,它也成就了AOP的实现;
参考链接:https://segmentfault.com/a/1190000042417419
BeanPostProcessor接口中定义了两个方法:
public interface BeanPostProcessor {
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
postProcessBeforeInitialization
:字面意思”初始化bean对象之前执行的后处理器“,在对象初始化之前进行调用,会对接收的参数bean对象进行修改和替换
postProcessAfterInitialization
:字面意思”初始化bean对象之后执行的后处理器“,在对象初始化之后调用,会对接收的参数bean对象进行修改和替换
补充:有时候我们对bean对象进行增强时,一般只需要重写postProcessAfterInitialization
方法即可
InstantiationAwareBeanPostProcessor接口继承了BeanPostProcessor接口,表示该接口也是一个后处理器
public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
@Nullable
default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
return null;
}
default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
return true;
}
@Nullable
default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
throws BeansException {
return null;
}
}
postProcessBeforeInstantiation
:字面意思”实例化对象之前执行的后处理器“,生成bean对象需要经过”创建“过程,也就是对象实例化过程
postProcessAfterInstantiation
:字面意思”实例化对象之后执行的后处理器“,该方法的执行是在对象实例化之后,依赖注入之前
postProcessProperties
:字面意思”属性后处理器“,该方法的执行是在对象依赖注入之前
DestructionAwareBeanPostProcessor接口继承了BeanPostProcessor接口,表示该接口也是一个后处理器
public interface DestructionAwareBeanPostProcessor extends BeanPostProcessor {
void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException;
default boolean requiresDestruction(Object bean) {
return true;
}
}
postProcessBeforeDestruction
:字面意思”销毁bean对象之前执行的后处理器“,这里之所以说是bean对象,是因为bean对象的创建流程已经走完,此时的对象不再是普通的实例对象而是初始化后的bean对象
主启动类
@SpringBootApplication
public class TestSpringBootApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(TestSpringBootApplication.class, args);
context.close();
}
}
测试对象
@Component
public class TestBean implements InitializingBean {
private static final Logger log = LoggerFactory.getLogger(LifeCycleBean.class);
public LifeCycleBean() {
log.info("构造");
}
@Autowired
public void autowire(@Value("${JAVA_HOME}") String home) {
log.info("依赖注入: {}", home);
}
@PostConstruct
public void init() {
log.info("初始化");
}
@PreDestroy
public void destroy() {
log.info("销毁");
}
@Override
public void afterPropertiesSet() throws Exception {
log.info("InitializingBean接口中重写的afterPropertiesSet方法");
}
}
对测试对象TestBean自定义后处理器
@Component
public class MyBeanPostProcessor implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor {
private static final Logger log = LoggerFactory.getLogger(MyBeanPostProcessor.class);
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
if (beanName.equals("testBean"))
log.info("<<<<<< 销毁之前执行, 如 @PreDestroy");
}
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
if (beanName.equals("testBean"))
log.info("<<<<<< 实例化之前执行, 这里返回的对象会替换掉原本的 bean");
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
if (beanName.equals("testBean")) {
log.info("<<<<<< 实例化之后执行, 这里如果返回 false 会跳过依赖注入阶段");
// return false;
}
return true;
}
@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
if (beanName.equals("testBean"))
log.info("<<<<<< 依赖注入阶段执行, 如 @Autowired、@Value、@Resource");
return pvs;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals("testBean"))
log.info("<<<<<< 初始化之前执行, 这里返回的对象会替换掉原本的 bean, 如 @PostConstruct、@ConfigurationProperties");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals("testBean"))
log.info("<<<<<< 初始化之后执行, 这里返回的对象会替换掉原本的 bean, 如代理增强");
return bean;
}
}
启动主启动类,控制台打印日志结果
创建
初始化
InitializingBean接口为bean提供了属性初始化后的处理方法,它只包括afterPropertiesSet方法,凡是继承该接口的类,在bean的属性初始化后都会执行该方法;
销毁