AbstractApplicationContext#registerBeanPostProcessors
调用PostProcessorRegistrationDelegate#registerBeanPostProcessors
这里主要注册bean后处理器, 注意与前面区分辨别, 前面的是实现了BeanFactoryProcessor接口bean工厂后处理器, 这里是实现BeanPostProcessor接口的bean处理器, BPP有两个接口方法postProcessBeforeInitialization和postProcessAfterInitialization, 名字取得都很形象见名知意, 前一个接口方法是在Bean实例化之后初始化之前调用, 后一个是初始化之后调用. spring里面的初始化和实例化和jvm是不一样的概念.
在jvm里面一个类会经历 loading->linking(verification->preparation->resolution)->initialization->...->unloading
, 就加载-连接-初始化最后卸载. 所以在jvm里面是先初始化之后才能实例化. 在spring中实例化就是创建对象. 初始化就是对象创建完毕后, 通过相关接口或者注解实现的接口回调功能.
// 这里的代码其实和上一章非常类似
public static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
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.
// 记录bean后处理器总数, +1 的原因在下一句, 新创建了一个BeanPostProcessorChecker加入到容器
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
// Separate between BeanPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
// 和上一章一样区分实现了PriorityOrdered, Ordered优先级接口的类
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);
}
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
else {
nonOrderedPostProcessorNames.add(ppName);
}
}
// First, register the BeanPostProcessors that implement PriorityOrdered.
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
// Next, register the BeanPostProcessors that implement 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);
// Now, register all regular 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);
// Finally, re-register all internal 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).
// 这是用来检测创建的bean是否实现了ApplicationListener接口
// 在它的postProcessAfterInitialization会将实现了ApplicationListener的bean放入到上下文中
// 同时在bean销毁的时候会调用它实现的DestructionAwareBeanPostProcessor#postProcessBeforeDestruction
// 从ApplicationEventMulticaster中移除
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}
总结:
- 提一点上面最后一行代码的ApplicationListenerDetector
@w=600
从类继承图可以看到此类实现了DestructionAwareBeanPostProcessor接口, DABPP接口继承自基础的BeanProstProcessor接口,前面已经说了BPP接口的作用, DestructionAwareBeanPostProcessor接口提供了两个方法:- postProcessBeforeDestruction此方法是bean在即将被销毁的时候的回调方法
- requiresDestruction决定这个bean实例是否需要被销毁