上一节 Spring5 源码阅读笔记(1.2) invokeBeanFactoryPostProcessors(beanFactory) 调用Bean工厂后置处理器 介绍了BeanFactoryPostProcessor,这一节介绍BeanPostProcessor。
如果上一节理解清楚了,那么这一节就很容易理解了。因此,本小节篇幅较短。
首先回顾一下 registerBeanPostProcessors 这个方法的作用:
源码中的注释是:Register bean processors that intercept bean creation.
翻译过来就是:注册拦截 bean 创建的 bean 处理器。
注意:注释里是没有 post 的。而且这个 BeanPostProcessor 实际上也不是对 bean 进行后置处理。它可以在 bean 实例化前后都进行操作。所以把 BeanPostProcessor 理解成 bean 的后置处理器并不准确。但是既然源码里它就叫 BeanPostProcessor,所以我们通常也叫它 bean 后置处理器。
当然,如果理解成是对 BeanDefinition 做后置处理,就没有问题了。
跟源码:
类 AbstractApplicationContext:
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}
跟 registerBeanPostProcessors:
类 PostProcessorRegistrationDelegate
public static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
//拿到工程里面所有实现了BeanPostProcessor接口的类,获取到BeanDefinition的名称
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的、和其余的BeanPostProcessor
//处理逻辑和上一节对BeanFactoryPostProcessor的处理逻辑大同小异,就不详细介绍了。
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
//装填容器,实例化、排序、注册 实现了PriorityOrdered接口的BeanPostProcessor
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
//这里就实例化了
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
priorityOrderedPostProcessors.add(pp);
//判断类型是否是MergedBeanDefinitionPostProcessor,如果是则代码是内部使用的
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
else {
nonOrderedPostProcessorNames.add(ppName);
}
}
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
//实例化、排序、注册 实现了Ordered接口的BeanPostProcessor
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);
//这里要看一下,与1.4.2节有联系,看到那里可以回过来看看
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
// 实例化、排序、注册 其它的的BeanPostProcessor
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).
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}
和上一节的 invokeBeanFactoryPostProcessors 方法相比:
相同点:都进行了排序,都进行了实例化(在其他 bean 之前实例化)
不同点:对 BeanFactoryPostProcessor 进行了调用,但对 BeanPostProcessor 仅仅是注册。
为什么会有这个不同点呢?
BeanFactoryPostProcessor 是对 BeanDefinition 进行增修,所以可以直接调用。
而 BeanPostProcessor 是为了去操作后面 bean 实例化过程的,那个时候再调用。
跟 registerBeanPostProcessors:
跟 addBeanPostProcessor:
类 ConfigurableBeanFactory
类 AbstractBeanFactory:
@Override
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
// Remove from old position, if any
this.beanPostProcessors.remove(beanPostProcessor);
// Track whether it is instantiation/destruction aware
//与1.4.2.1节有联系,看到那里可以回过来看看。
//在这里,hasInstantiationAwareBeanPostProcessors置为true。
//至于为什么会有这样的BeanPostProcessor,在1.1.2末尾有答案。
if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
this.hasInstantiationAwareBeanPostProcessors = true;
}
if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
this.hasDestructionAwareBeanPostProcessors = true;
}
// Add to end of list
this.beanPostProcessors.add(beanPostProcessor);
}