JAVA && Spring && SpringBoot2.x — 学习目录
BeanPostProcessor在字面上理解为:Bean的后置处理器,允许Spring框架在创建Bean时对其进行定制化的修改。
经典的应用:初始化Bean时创建代理对象,初始化Bean时读取@Autowired等注解。
注意点:BeanPostProcessor
本身也是一个Bean
,而它的初始化时机一般要早于普通的Bean
。如高优先级BeanPostProcessor
中依赖一些普通Bean
时,可能会造成普通的Bean
实例化时机早于低优先级BeanPostProcessor
,造成低优先的BeanPostProcessor
不能生效。
1. 自定义实现BeanPostProcessor接口
@Slf4j
@Component
public class MyBeanPostProcessor implements BeanPostProcessor,Ordered{
//测试用,在自定义BeanPostProcessor中引入普通Bean对象
@Autowired
private AccountImpl account;
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
//实例化、依赖注入完毕,在调用显示的初始化之前完成一些定制的初始化任务
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (beanName.contains("aServiceImpl")) {
log.info("【bean初始化之前,bean is {},beanName is {}】", bean.toString(), beanName);
}
return bean;
}
//实例化、依赖注入、初始化完毕时执行
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (beanName.contains("aServiceImpl")) {
log.info("【bean初始化之后,bean is {},beanName is {}】", bean.toString(), beanName);
}
return bean;
}
}
注:接口的两个方法都不能返回null,因为后置处理器从Spring IOC容器中获取Bean实例对象没有再次放回到IOC容器中,后续操作获取Bean对象可能会空指针异常!
启动项目后,在调用Bean对象时,发现本应被代理的Bean对象没有被代理。
实际上,在自定义的BeanPostProcessor中依赖注入Bean对象,那么Bean对象没有经过代理的BeanPostProcessor,造成问题。那么BeanPostProcessor的加载优先级如何呢?
2. BeanPostProcessor加载的优先级
一个Bean可能包含多个后置处理器,如何控制不同的BeanPostProcessor
的加载顺序呢?
org.springframework.context.support.PostProcessorRegistrationDelegate#registerBeanPostProcessors(ConfigurableListableBeanFactory, AbstractApplicationContext)
中,对BeanPostProcessor的加载分成了4个部分:
public static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
// 从容器中获取所有的BeanPostProcessor类型的BeanName数组
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
// Register BeanPostProcessorChecker that logs an info message when
// a bean is created during BeanPostProcessor instantiation, i.e. when
// a bean is not eligible for getting processed by all BeanPostProcessors.
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
// 分离实现了PriorityOrdered,Ordered 接口的BeanPostProcessors 和其他类型的BeanPostProcessors
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
priorityOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
// 如果实现了 Ordered 接口,则将全类名添加到orderedPostProcessorNames的List中
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
// 否则添加全类名到 nonOrderedPostProcessorNames 的List 中
else {
nonOrderedPostProcessorNames.add(ppName);
}
}
//首先,注册实现了PriorityOrdered接口的接口
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
// 然后,注册实现了Ordered 接口的后置处理器
List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
for (String ppName : orderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
orderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
sortPostProcessors(orderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
// 注册所有常规的 BeanPostProcessors
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
for (String ppName : nonOrderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
nonOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
// 最后, 再次注册所有的内部BeanPostProcessors
sortPostProcessors(internalPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, internalPostProcessors);
// Re-register post-processor for detecting inner beans as ApplicationListeners,
// moving it to the end of the processor chain (for picking up proxies etc).
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}
- 加载实现
PriorityOrdered
接口的BeanPostProcessor类; - 加载实现
Ordered
接口的BeanPostProcessor类; - 加载
BeanPostProcessor
类; - 加载实现
MergedBeanDefinitionPostProcessor
接口的BeanPostProcessor类;
高优先级的BeanPostProcessor
类可以影响低优先级的BeanPostProcessor
类,但是同优先级的BeanPostProcessor
不能相互作用。
即若是Ordered级别的BeanPostProcessor实现引用了Bean对象,那么该Bean对象将失去所有Order级别后置处理器的处理。
因此在实现BeanPostProcessor
接口时,应注意加载顺序的影响。
3. Spring的初始执行顺序
BeanFactoryPostProcessor的postProcessBeanFactory方法
构造方法!
BeanPostProcessor的postProcessBeforeInitialization方法
声明@PostConstruct注解的方法
InitializingBean接口的afterPropertiesSet()方法
声明的init方法!
BeanPostProcessor的postProcessAfterInitialization方法
容器启动—CommandLineRunner接口方法!
BeanPostProcessor的两个处理方法,是在init方法前后进行处理的。
推荐阅读
BeanPostProcessor加载次序及其对Bean造成的影响分析