Spring容器初始化之invokeBeanFactoryPostProcessors()源码分析

>>强大,10k+点赞的 SpringBoot 后台管理系统竟然出了详细教程!
invokeBeanFactoryPostProcessors方法会实例化所有 BeanFactoryPostProcessor 以及其子类 BeanDefinitionRegistryPostProcessor。这两个类主要针对的是BeanFactory的,其作用是在 BeanDefinition 创建之后,Bean 实例化对象前对 BeanDefinition 对象相关属性进行修改;另外还有一个类叫 BeanPostProcessor,该类主要针对的是 Bean 操作的。关于这两个类的区别我会在下一篇内容介绍。
1. 在讲AbstractApplicationContext.refresh()invokeBeanFactoryPostProcessors开始之前。在讲之前先简单说下为啥没有讲prepareBeanFactorypostProcessBeanFactory两个方法,因为这两个方法都特别简单,第一个方法是为BeanFactory设置各种各样的属性,包括一些BeanPostProcessor的设置(BeanPostProcessor后面会专门讲解)等等,第二个方法没有具体实现,是为开发者留出扩展的。
AbstractApplicationContext.invokeBeanFactoryPostProcessors()方法实现
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
// 获取context中已添加的beanFactoryPostProcessors
// 实例化并调用所有BeanFactoryPostProcessors
// 主要实现也是在该方法里面进行实现
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
// Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
// (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
}

2. 该方法的主要实现在invokeBeanFactoryPostProcessors方法里面进行实现的。虽然这个方法特别长,但是逻辑很简单,耐心看下去
  • BeanFactoryPostProcessor:一切处理BeanFactory的父接口
  • BeanDefinitionRegistryPostProcessor:实现了BeanFactoryPostProcessor接口的接口

PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors()方法实现
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors)
{
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
Set<String> processedBeans = new HashSet<>();
// Step1:beanFactory是BeanDefinitionRegistry接口或者是其子类
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
// Step2:存放普通的beanFactoryPostProcessors
List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<>();
// Step3:存放beanDefinitionRegistryPostProcessors
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new LinkedList<>();
// Step4:首先处理接口传进来的factoryPostProcessors
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
// Step5:实现了BeanDefinitionRegistryPostProcessor接口的factoryPostProcessor
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
// Step6:直接调用其实现
registryProcessor.postProcessBeanDefinitionRegistry(registry);
// Step7:添加到registryProcessors里面,后面调用 BeanFactoryPostProcessor.postProcessBeanFactory()方法
registryProcessors.add(registryProcessor);
}
else {
// Step8:非BeanFactoryProcessor及其子类的,直接添加到regularPostProcessors里面,后面调用执行postProcessorBeanFactory方法
regularPostProcessors.add(postProcessor);
}
}
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let the bean factory post-processors apply to them!
// Separate between BeanDefinitionRegistryPostProcessors that implement
// PriorityOrdered, Ordered, and the rest.
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
// Step9:获取所有BeanDefinitionRegistryPostProcessor实现的子类并且实现了PriorityOrdered接口的factoryPostProcessor
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
// Step10:排序
sortPostProcessors(currentRegistryProcessors, beanFactory);
// Step11:添加到registryProcessors中,后面调用postProcessorBeanFactory方法
registryProcessors.addAll(currentRegistryProcessors);
// Step12:遍历执行每个factoryPostProcessor的postProcessBeanDefinitionRegistry方法
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
// Step13:清空
currentRegistryProcessors.clear();
// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
// Step14:获取所有BeanDefinitionRegistryPostProcessor并且实现了Ordered接口的factoryPostProcessor
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
// Step15:排序
sortPostProcessors(currentRegistryProcessors, beanFactory);
// Step16:添加到registryProcessors中,后面调用postProcessorBeanFactory方法
registryProcessors.addAll(currentRegistryProcessors);
// Step17:遍历每个factoryPostProcessor的postProcessorBeanDefinitionRegistry方法
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
// Step18:清空
currentRegistryProcessors.clear();
// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
// Step19:获取所有实现了BeanDefinitionRegistryPostProcessor接口的类,且没有实现PriorityOrdered接口和Ordered接口的类
boolean reiterate = true;
while (reiterate) {
reiterate = false;
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
reiterate = true;
}
}
// Step20:排序
sortPostProcessors(currentRegistryProcessors, beanFactory);
// Step21:添加到registryProcessors中,后面调用PostProcessorBeanFactory方法
registryProcessors.addAll(currentRegistryProcessors);
// Step22:遍历每个factoryPostProcessor的PostProcessorBeanDefinitionRegistry方法
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
}
// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
// Step23:所有实现了BeanDefinitionRegistryPostProcessor接口的类,调用beanFactoryPostProcessors方法
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
// Step24:所有实现了BeanFactoryPostProcessor接口的类,调用BeanFactoryPostProcessor方法
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
}
else {
// Invoke factory processors registered with the context instance.
// Step26:所有非BeanDefinitionRegistry接口及其子类的工厂,直接调用BeanFactoryPostProcessor方法
invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
}
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let the bean factory post-processors apply to them!
// Step27:获取所有实现了BeanFactoryPostProcessor接口的类名
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {
// Step28:跳过已经invoke的factoryPostProcessor
if (processedBeans.contains(ppName)) {
// skip - already processed in first phase above
}
// Step29:实现了BeanFactoryPostProcessor接口并且实现了PriorityOrdered接口的类
else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
}
// Step30:实现了BeanFactoryPostProcessor并且实现了Ordered接口的类
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
else {
// Step31:普通的BeanFactoryPostProcessor类
nonOrderedPostProcessorNames.add(ppName);
}
}
// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
// Step32:排序
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
// Step33:调用BeanFactoryPostProcessor方法
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
// Step34:实现了Ordered接口排序并调用BeanFactoryPostProcessor方法
List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>();
for (String postProcessorName : orderedPostProcessorNames) {
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
sortPostProcessors(orderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
// Finally, invoke all other BeanFactoryPostProcessors.
// Step35:普通的BeanFactoryPostProcessor调用beanFactoryPostProcessor方法
List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
for (String postProcessorName : nonOrderedPostProcessorNames) {
nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
// Clear cached merged bean definitions since the post-processors might have
// modified the original metadata, e.g. replacing placeholders in values...
beanFactory.clearMetadataCache();
}

总结

  1. 先加载预先在上线文中定义的BeanFactoryPostProcessor

  2. 然后加载实现了BeanDefinitionRegistryPostProcessor接口的BeanFactoryPostProcessor

  3. 然后加载实现了BeanFactoryPostProcessor接口的类

  4. 在 Spring 和 mybatis 整合中,MapperScannerConfigurer.java 就实现了BeanDefinitionRegistryPostProcessor接口,在其实现的方法postProcessorBeanDefinitionRegistry中,扫描basePackage指定的路径,将该目录下的类封装成BeanDefinition 并加载到BeanFactory中。所以项目中的接口不需要配置在 XML 文件中或者用注解声明,而可以直接使用 Dao 类对应的实例化对象。再比如PropertySourcesPlaceholderConfigurer(实现了context:property-placeholder的功能),主要作用是替换掉 ${} 占位符的内容,其重写了postProcessorBeanFactory方法


讲完invokeBeanFactoryPostProcessors相关实现后,下一篇我们来看看registerBeanPostProcessors都实现了些什么功能,它和本篇文章将的invokeBeanFactoryPostProcessors方法有什么区别。



Spring容器初始化之invokeBeanFactoryPostProcessors()源码分析

欢迎关注我,共同学习


原文始发于微信公众号(z小赵):Spring容器初始化之invokeBeanFactoryPostProcessors()源码分析