1.BeanFactoryPostProcessors和BeanDefinitionRegistryPostProcessor的作用
2.spring源码prepareBeanFactory(beanFactory)流程介绍
3.spring源码prepareBeanFactory(beanFactory)源码解析
4.总结
1.BeanFactoryPostProcessors和BeanDefinitionRegistryPostProcessor的作用
github源码地址(带正文):
https://github.com/su15967456…
spring源码执行流程图:
咱们点到这个办法里,大抵浏览一下代码,发现次要是围绕着这两个汇合进行操作:
简而言之:
invokeBeanFactoryPostProcessors办法会把所有实现beanFactoryPostProcessor和BeanDefinitionRegistryPostProcessor的类进行实例化和调用。
咱们先来看一下beanFactoryPostProcessor的正文:
意思大抵为:在bean definitions全副加载结束,并且在初始化之前,beanFactoryPostProcessor能够对这些bd重写或者减少一些属性。
咱们再来看一下BeanDefinitionRegistryPostProcessor的正文:
当bean definitions全副被加载结束,并且在初始化之前,BeanDefinitionRegistryPostProcessor减少一些额定的bean definition。
咱们得出结论:
BeanDefinitionRegistryPostProcessor:能够用来减少新的bean Difinition
beanFactoryPostProcessor:能够对bean Difinition的进行批改。
2.spring源码prepareBeanFactory(beanFactory)流程介绍
从整体的执行程序来看,这个办法的执行流程是这个样子的:
1)执行内部传进来的BeanFactoryPostProcessor类
2)执行实现子类BeanDefinitionRegistryPostProcessor接口的类
3)执行实现父类BeanFactoryPostProcessor接口的类
从执行每一步的BeanDefinitionRegistryPostProcessor或者BeanFactoryPostProcessor,又能够分为以下几个逻辑:
1)执行实现了PriorityOrdered(高优先级)的类
2)执行实现了Ordered(有序)的类
3)执行什么都没有实现的一般类
3.spring源码prepareBeanFactory(beanFactory)源码解析
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
// WARNING: Although it may appear that the body of this method can be easily
// refactored to avoid the use of multiple loops and multiple lists, the use
// of multiple lists and multiple passes over the names of processors is
// intentional. We must ensure that we honor the contracts for PriorityOrdered
// and Ordered processors. Specifically, we must NOT cause processors to be
// instantiated (via getBean() invocations) or registered in the ApplicationContext
// in the wrong order.
//
// Before submitting a pull request (PR) to change this method, please review the
// list of all declined PRs involving changes to PostProcessorRegistrationDelegate
// to ensure that your proposal does not result in a breaking change:
// https://github.com/spring-projects/spring-framework/issues?q=PostProcessorRegistrationDelegate+is%3Aclosed+label%3A%22status%3A+declined%22
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
//无论是什么状况,先执行BeanDefinitionRegistryPostProcessors
//将曾经执行的BFPP存储在processBean中,避免反复执行
Set<String> processedBeans = new HashSet<>();
//BeanDefinitionRegistry是对beanDefinition进行操作的类
// 判断beanFactory是不是 BeanDefinitionRegistry的实现,此处是DefaultListableBeanFactory,实现了BeanDefinitionRegistry接口,此处为true
if (beanFactory instanceof BeanDefinitionRegistry) {
//类型转换
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
//此时要做一个辨别,BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子类
//BeanFactoryPostProcessor次要针对的对象是BeanFactory,
//BeanDefinitionRegistryPostProcessor次要针对的对象是BeanDefinition
//寄存BeanFactoryPostProcessor的汇合类
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
//寄存BeanDefinitionRegistryPostProcessor的汇合
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
//首先解决入参中的beanFactoryPostProcessors,遍历所有的beanFactoryPostProcessors
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
//如果是BeanDefinitionRegistryPostProcessor
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
//查找BeanDefinitionRegistryPostProcessor中的postProcessBeanDefinitionRegistry办法
registryProcessor.postProcessBeanDefinitionRegistry(registry);
//增加到registryProcessors
registryProcessors.add(registryProcessor);
} else {
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.
//用于保留本次要执行的BeanDefinitionRegistryPostProcessor
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
//调用所有实现PriorityOrdered接口的BeanDefinitionRegistryPostProcessor实现类
//找到所有实现BeanDefinitionRegistryPostProcessor接口bean的beanName
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
//遍历所有合乎规定的postProcessNames
for (String ppName : postProcessorNames) {
//检测是否实现了PriorityOrdered接口
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
//获取名字对应的bean实例,增加到currentRegistryProcessor
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
//将要被执行的BFPP增加到processedBeans中,避免反复执行
processedBeans.add(ppName);
}
}
//依照优先程序进行排序
sortPostProcessors(currentRegistryProcessors, beanFactory);
//增加到registryProcessors中,用于最初执行postProcessBeanFactory办法
registryProcessors.addAll(currentRegistryProcessors);
//遍历currentRegistryProcessors,执行postProcessBeanFactory办法
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
//执行结束后,清空
currentRegistryProcessors.clear();
// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
//调用所有实现Ordered接口的BeanDefinitionRegistryPostProcessor实现类
//找到所有实现BeanDefinitionRegistryPostProcessor接口bean的beanName
//为什么要从新获取 :
//下面调用invoke办法的时候,可能会新增一些 BeanDefinitionRegistryPostProcessor
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
//检测是否实现了Order接口,并且还未执行过程
if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
//获取名字的bean实例,增加到currentRegistryProcessors
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
//增加到已执行过的processedBeans
processedBeans.add(ppName);
}
}
//依照优先程序进行排序
sortPostProcessors(currentRegistryProcessors, beanFactory);
//增加到registryProcessors中,用于最初执行postProcessBeanFactory办法
registryProcessors.addAll(currentRegistryProcessors);
//遍历currentRegistryProcessors,执行postProcessBeanFactory办法
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
//执行结束后,清空
currentRegistryProcessors.clear();
// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
// 最初,调用剩下的BeanDefinitionRegistryPostProcessors,没有实现Order的
boolean reiterate = true;
while (reiterate) {
reiterate = false;
//找出所有BeanDefinitionRegistryPostProcessors的接口类
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
//如果还未执行过BeanDefinitionRegistryPostProcessors
if (!processedBeans.contains(ppName)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
//如果在中途过程中,可能会新增 BeanDefinitionRegistryPostProcessors,所以这里要为true
reiterate = true;
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
currentRegistryProcessors.clear();
}
// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
//执行postProcessBeanFactory
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
} else {
// Invoke factory processors registered with the context instance.
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!
//获取实现BeanFactoryPostProcessor的所有类
//到目前为止,所有BeanDefinitionRegistryPostProcessor曾经全副实现结束了,接下来开始BeanFactoryPostProcessor的类的解决
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
// 他们不反复执行是因为beanFactoryPostProcessor不会新增新Processor
// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
//下面只执行了实现BeanDefinitionRegistryPostProcessor的postprocessor,并没有实现
//有priorityOrdered的PostProcessors汇合
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
//有ordered的PostProcessors汇合 为什么上面两种存string,下面那种存类
//代码改掉还是能够运行的,猜想可能是省空间
List<String> orderedPostProcessorNames = new ArrayList<>();
//没有order的PostProcessors汇合
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {
if (processedBeans.contains(ppName)) {
// skip - already processed in first phase above
} else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
} else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
} else {
nonOrderedPostProcessorNames.add(ppName);
}
}
// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
//依据priorityOrderedPostProcessors的汇合先排序,后执行
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
for (String postProcessorName : orderedPostProcessorNames) {
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
sortPostProcessors(orderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
// Finally, invoke all other BeanFactoryPostProcessors.
List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
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)为什么执行子类BeanDefinitionRegistryPostProcessor的时候,每次都要从容器中从新获取类?
因为实现BeanDefinitionRegistryPostProcessor的类能够减少新的bd,也就是说能够减少新的BeanDefinitionRegistryPostProcessor,所以每次都要从新获取。
2)为什么执行父类BeanFactoryPostProcessor的时候,不必从新获取?
因为父类BeanFactoryPostProcessor,不能减少新的bd,所以就不必从新获取了。
3)
4.总结
咱们能够看出,这个办法的执行逻辑还是比较简单和容易了解,而且都是由一个函数编写而成,封装地不是太厉害。
发表回复