1. 引子
之前在阅读Spring的源码的时候,经常会看到BeanPostProcessor,很多书中都叫它Bean后置处理器。但是这个到底是什么意思呢,还是很迷糊。直到在简书上看到了一篇文章:https://www.jianshu.com/p/f607ecc99c00,才慢慢理解这个类的含义。也借这篇文章,对知识进行梳理梳理。
2. 示例
下面通过一个demo来了解BeanPostProcessor。新建一个Spring Boot应用作为框架。
2.1 普通bean对象
首先编写一个普通的Bean对象:
@Component
public class CommonBean {
private static final Logger log = LoggerFactory.getLogger(CommonBean.class);
public CommonBean(){
log.info("construct CommonBean");
}
}
2.2 自定义BeanPostProcessor
然后再写一个BeanPostProcessor,这是一个接口,下面写一个它的实现类:
@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {
private static final Logger log = LoggerFactory.getLogger(CustomBeanPostProcessor.class);
public CustomBeanPostProcessor() {
log.info("construct CustomBeanPostProcessor");
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof Bean4BBP) {
log.info("process bean before initialization");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof Bean4BBP) {
log.info("process bean after initialization");
}
return bean;
}
}
写完这两个类后,就直接启动Spring Boot应用。
启动后看到日志打印如下:
construct CustomBeanPostProcessor
construct CommonBean
process bean before initialization
process bean after initialization
从这四条日志的顺序可以看出,首先创建BeanPostProcessor对象,然后创建了我们自定义的普通对象,最后才依次执行了BeanPostProcessor的postProcessBeforeInitialization方法和postProcessAfterInitialization方法。为了更加清楚的证实这一点,下面在这两个方法中加上断点进行调试,发现Spring的源码执行到了AbstractAutowireCapableBeanFactory的initializeBean方法中,下面就进行分析。
2.3 源码分析
代码如下:
protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
Object wrappedBean = bean;
wrappedBean = this.applyBeanPostProcessorsBeforeInitialization(bean, beanName);
try {
this.invokeInitMethods(beanName, wrappedBean, mbd);
} catch (Throwable var6) {
throw new BeanCreationException(...);
}
wrappedBean = this.applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
return wrappedBean;
}
该方法的applyBeanPostProcessorsBeforeInitialization方法和applyBeanPostProcessorsAfterInitialization方法中分别调用了BeanPostProcessor的postProcessBeforeInitialization方法和postProcessAfterInitialization方法。以applyBeanPostProcessorsBeforeInitialization方法为例:
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException {
Object result = existingBean;
Object current;
for(Iterator var4 = this.getBeanPostProcessors().iterator(); var4.hasNext(); result = current) {
BeanPostProcessor processor = (BeanPostProcessor)var4.next();
current = processor.postProcessBeforeInitialization(result, beanName);
if (current == null) {
return result;
}
}
return result;
}
内部实现就是遍历所有的BeanPostProcessor,然后依次执行postProcessBeforeInitialization方法。
那夹在before和after方法中间的invokeInitMethods方法是干嘛的呢?
protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBeanDefinition mbd) throws Throwable {
// 首先判断一个bean是否实现InitializingBean接口
boolean isInitializingBean = bean instanceof InitializingBean;
// 如果实现了InitializingBean接口接口以及afterPropertiesSet方法
if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
...
//调用每个bean的afterPropertiesSet方法
((InitializingBean)bean).afterPropertiesSet();
...
}
}
从注释中可以看到这个方法的逻辑了,下面就修改改下之前的CommonBean,让其实现InitializingBean接口。
2.4 实现InitializingBean接口
@Component
public class CommonBean implements InitializingBean {
private static final Logger log = LoggerFactory.getLogger(CommonBean.class);
public CommonBean(){
log.info("construct CommonBean");
}
@Override
public void afterPropertiesSet() throws Exception {
log.info("init CommonBean");
}
}
可以看到又实现了afterPropertiesSet方法,加上后,日志打印如下:
construct CustomBeanPostProcessor
construct CommonBean
process bean before initialization
init CommonBean
process bean after initialization
从日志中看到在before和after之间多执行了afterPropertiesSet方法,说明前面的源码分析是对的。
一般在afterPropertiesSet方法中做的是:对象创建完之后,针对对象的一些初始化操作。
3. 小结
总的来说,顺序是这样的:Spring启动的时候,会先创建所有的Bean对象,然后再调用BeanPostProcessor的before和After方法,如果Bean实现了InitializingBean接口,那么还会调用afterPropertiesSet方法来设置属性等操作。
那BeanPostProcessor的before和After方法中一般都做些什么呢?参考的文章中提到,还和Spring AOP的实现有关,这里就不展开了,后面学习Spring AOP的源码再深入分析吧。