代码:
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
//执行BeanFactoryPostProcessors
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
// 如何找到一个LoadTimeWeaver(代码织入),那么就准备将后置处理器“织入”bean工厂
// (例如,一个 @Bean 方法通过ConfigurationClassPostProcessor来注册)
if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
}
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
// 首先调用BeanDefinitionRegistryPostProcessors
Set<String> processedBeans = new HashSet<>(); //用于存放已经执行过的BeanDefinitionRegistryPostProcessor实现类名称
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
//处理手动添加的BeanFactoryPostProcessor的实现。 (调用AbstractApplicationContext.addBeanFactoryPostProcessor设置进去的,支持情况不会出现)
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
//如果是BeanDefinitionRegistryPostProcessor的实现
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
//直接执行postProcessBeanDefinitionRegistry方法
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);
}
else {
//添加到regularPostProcessors
regularPostProcessors.add(postProcessor);
}
}
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
//从bean中找到所有BeanDefinitionRegistryPostProcessor的实现类,添加到currentRegistryProcessors中
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
//实现了PriorityOrdered接口
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
//排序
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
/**
* 这里会调用 {@link ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry} 方法,
* 会扫描@ComponentScan注解配置的路径下所有的需要交给容器管理的bean(@bean等注解),并注入到ioc容器中
* 还会处理@Import等注解
*/
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
//从ioc容器中找实现了Ordered的BeanDefinitionRegistryPostProcessor的实现类,执行postProcessBeanDefinitionRegistry方法
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
//不在processedBeans中(没有在上一步执行) 且 实现了Ordered接口
if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
//最后,调用所有其他BeanDefinitionRegistryPostProcessors直到没有其他的出现。
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;
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
}
//现在,调用到目前为止处理过的所有处理器的postProcessBeanFactory回调。(开始执行BeanFactoryPostProcessor接口实现类的方法)
//registryProcessors里面存放的是BeanDefinitionRegistryPostProcessors的实现类
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
//regularPostProcessors中存放的是BeanFactoryPostProcessor的实现类
// (注意:这里面的是手动提供给spring的,不是spring扫描得到的实现类)
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
}
else {
//调用注册到上下文实例的工厂处理器。
invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
}
//获取spring扫描出来的(程序员编写的)BeanFactoryPostProcessor的实现类
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
/** 扫描出来的BeanFactoryPostProcessor的实现类,且实现了PriorityOrdered接口 */
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
/** 扫描出来的BeanFactoryPostProcessor的实现类名,且实现了Ordered接口 */
List<String> orderedPostProcessorNames = new ArrayList<>();
/** 扫描出来的BeanFactoryPostProcessor的实现类名,没有实现Ordered接口和PriorityOrdered接口 */
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {
if (processedBeans.contains(ppName)) {
//这个是子类BeanDefinitionRegistryPostProcessor的实现类,之前已经执行过postProcessBeanFactory方法,跳过
}
else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
//扫描出来的BeanFactoryPostProcessor的实现类,且实现了PriorityOrdered接口
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
//扫描出来的BeanFactoryPostProcessor的实现类,且实现了Ordered接口
orderedPostProcessorNames.add(ppName);
}
else {
//扫描出来的BeanFactoryPostProcessor的实现类,没有实现Ordered接口和PriorityOrdered接口
nonOrderedPostProcessorNames.add(ppName);
}
}
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
//执行BeanFactoryPostProcessor的实现类的postProcessBeanFactory方法
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
for (String postProcessorName : orderedPostProcessorNames) {
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
sortPostProcessors(orderedPostProcessors, beanFactory);
//执行BeanFactoryPostProcessor的实现类的postProcessBeanFactory方法
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
for (String postProcessorName : nonOrderedPostProcessorNames) {
nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
//执行BeanFactoryPostProcessor的实现类的postProcessBeanFactory方法
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
beanFactory.clearMetadataCache();
}
执行流程:
①执行容器中BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry实现方法,并且实现了PriorityOrdered接口(这时候执行的是spring提供的实现类,会扫描注解生成BeanDefinition放到ioc容器)
②执行容器中BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry实现方法,并且实现了Ordered接口(这时候执行的上一步扫描到容器中的实现类)
③执行容器中BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry实现方法(这时候执行的是容器中的剩下没执行的实现类)
④执行容器中BeanFactoryPostProcessor.postProcessBeanFactory实现方法,并且实现了PriorityOrdered接口(执行的是容器中的实现类)
⑤执行容器中BeanFactoryPostProcessor.postProcessBeanFactory实现方法,并且实现了Ordered接口(执行的是容器中的实现类)
⑥执行容器中BeanFactoryPostProcessor.postProcessBeanFactory实现方法(这时候执行的是器中剩下的实现类)
注意:BeanFactoryPostProcessor是BeanDefinitionRegistryPostProcessor的父接口,执行456步的时候也会执行BeanDefinitionRegistryPostProcessor.postProcessBeanFactory方法
下一章:六、registerBeanPostProcessors方法解析