BeanPostProcessor
也称为Bean后置处理器,它是Spring中定义的接口,在Spring容器的创建过程中(具体为Bean初始化前后)会回调BeanPostProcessor
中定义的两个方法。
public interface BeanPostProcessor {
//在bean初始化之前执行
实现原理:探查源码
1)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
在创建Bean时,会调起org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition)进行初始化
这时会执行applyBeanPostProcessorsBeforeInitialization和applyBeanPostProcessorsAfterInitialization,所以可以通过这两个方法对bean进行包装
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException {
Object result = existingBean;
Object current;
//遍历当时容器中所有的BeanPostProcessor 一般都是遍历完所有的BeanPostProcessor
for(Iterator var4 = this.getBeanPostProcessors().iterator(); var4.hasNext(); result = current) {
BeanPostProcessor processor = (BeanPostProcessor)var4.next();
current = processor.postProcessBeforeInitialization(result, beanName);
//返回的是null那么我们通过getBean方法将得不到目标Bean ,退出遍历
if (current == null) {
return result;
}
}
return result;
}
public Object applyBeanPostProcessorsAfterInitialization(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.postProcessAfterInitialization(result, beanName);
if (current == null) {
return result;
}
}
return result;
}
应用场景:
1、解析bean的注解,将注解中的字段转化为属性
2、统一将属性在执行前,注入bean中,如数据库访问的sqlMap,如严重服务,这样不需要每个bean都配置属性
3、打印日志,记录时间等。
常见BeanPostProcessor分析
ApplicationContextAwareProcessor
什么时候被注入的?
org.springframework.context.support.AbstractApplicationContext#refresh核心方法
//准备beanFactory
this.prepareBeanFactory(beanFactory);
org.springframework.context.support.AbstractApplicationContext#prepareBeanFactory
//往beanFactory添加ApplicationContextAwareProcessor
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
//往beanFactory添加ApplicationListenerDetector
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
作用:
org.springframework.context.support.ApplicationContextAwareProcessor#postProcessBeforeInitialization
org.springframework.context.support.ApplicationContextAwareProcessor#invokeAwareInterfaces
实现了某种Aware接口,就会注入相关属性
例如:bean实现了ApplicationContextAware接口
if (bean instanceof ApplicationContextAware) { //会调用bean重写的setApplicationContext方法,注入applicationContext ((ApplicationContextAware)bean).setApplicationContext(this.applicationContext); }