【接上文】
前面用三篇介绍了对一系列注解的解析过程,下面我们再回到最初的那个方法,看下整体脉络。
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
Set<String> processedBeans = new HashSet<>();
// 通过上面的代码可以知道beanFactory的实现类其实是DefaultListableBeanFactory,我们暂且只看下这个类的定义。
// public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
// implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable
// 可以看到这里的DefaultListableBeanFactory是实现了BeanDefinitionRegistry接口的,这里的if判断结果是true
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
//这里的beanFactoryPostProcessor 会不会包含自定义的beanFactoryPostProcessor呢
//大概猜测下 这里之前的步骤并没有检测beanFactoryPostProcessor的过程,这里如果有的话就是系统内置的beanFactoryPostProcessor
//在AbstractApplicationContext中传参使用的是默认的值 也就是新new出来的一个list 这里的for循环目前是不会走里面的内容的。
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
registryProcessor.postProcessBeanDefinitionRegistry(registry);
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.
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
//CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME --> ConfigurationClassPostProcessor -->实现了BeanDefinitionRegistryPostProcessor,PriorityOrdered
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
//所以这里取到的beanName是CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME这个常量对应的值
for (String ppName : postProcessorNames) {
//判断这个bean是否实现了PriorityOrdered接口 显然这里是返回true的
//NOTE:前面的分析中提到过可以用户自定义特殊beanFactory的操作 所以这里的循环和if判断都是有价值的
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
//beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)获取到的就是ConfigurationClassPostProcessor
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
//通过beanFactory.getBean的方法调用,这里的bean已经在容器中真实存在了, 也就是ConfigurationClassPostProcessor
processedBeans.add(ppName);
}
}
//对后置处理器进行排序
sortPostProcessors(currentRegistryProcessors, beanFactory);
//加入到已注册的后置处理器集合中
registryProcessors.addAll(currentRegistryProcessors);
//这里真正调用BeanDefinitionRegistryPostProcessors后置处理器的方法。
//这里currentRegistryProcessors里面装的就是ConfigurationClassPostProcessor
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
currentRegistryProcessors.clear();
// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
//上面invokeBeanDefinitionRegistryPostProcessors这个方法的内容已经详细分析过了,可以知道一些特定的bean已经被解析并加入到容器中了。
//这里再通过BeanDefinitionRegistryPostProcessor这个类型去获取bean的时候已经可以获取到自定义的BeanDefinitionRegistryPostProcessor的实现了。
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);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
//这里又调用了一次这个方法
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
currentRegistryProcessors.clear();
// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
//这里通过一个while循环迭代所有的BeanDefinitionRegistryPostProcessor,生成bean并放入容器中
boolean reiterate = true;
while (reiterate) {
reiterate = false;
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
//与上面代码的区别是这里没有了beanFactory.isTypeMatch(ppName, Ordered.class)这个判断
// 结合上面两套类似的逻辑,可以发现这里其实把BeanDefinitionRegistryPostProcessor分成了三类,
// 就是1.实现了PriorityOrdered 2.实现了Ordered接口 3. 其他接口 按顺序依次处理1 2 3 的情况
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, beanFactory.getApplicationStartup());
currentRegistryProcessors.clear();
}
// 处理BeanFactoryPostProcessor的回调
// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
// 因为BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口,
// 这里的处理是先回调实现了BeanDefinitionRegistryPostProcessor接口的postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)方法,
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
// 再调用直接实现了BeanFactoryPostProcessor接口的postProcessBeanFactory方法。
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!
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<>();
// 处理上面步骤中没有处理过的接口实现
// 2. Ordered
for (String ppName : postProcessorNames) {
if (processedBeans.contains(ppName)) {
// skip - already processed in first phase above
//如果上面处理过了,直接跳过。
}
//1. 实现了PriorityOrdered接口的 这里直接处理了成bean了,后面就处理了另外两种情况
else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
}
//2. 实现了Ordered接口的
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
//3. 其他情况
else {
nonOrderedPostProcessorNames.add(ppName);
}
}
// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
// 排序后调用他们的回调方法
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
for (String postProcessorName : orderedPostProcessorNames) {
//生成对应的bean后加入到对应的list中
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
sortPostProcessors(orderedPostProcessors, beanFactory);
//调用回调方法
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
// Finally, invoke all other BeanFactoryPostProcessors.
//生成其他情况的bean 并调用回调方法
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();
}
//invokeBeanFactoryPostProcessors
/**
* Invoke the given BeanFactoryPostProcessor beans.
* 跟想象中的一样,遍历调用接口回调方法
*/
private static void invokeBeanFactoryPostProcessors(
Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) {
for (BeanFactoryPostProcessor postProcessor : postProcessors) {
StartupStep postProcessBeanFactory = beanFactory.getApplicationStartup().start("spring.context.bean-factory.post-process")
.tag("postProcessor", postProcessor::toString);
postProcessor.postProcessBeanFactory(beanFactory);
postProcessBeanFactory.end();
}
}
到这里其实所有的BeanDefinitionRegistryPostProcessor和BeanFactoryPostProcessor都已经成为容器中的bean了。
【小结】
根据文中注释大概了解了整个流程脉络:
- 通过类型BeanDefinitionRegistryPostProcessor获取到内置的实现ConfigurationClassPostProcessor
- 调用ConfigurationClassPostProcessor的postProcessBeanDefinitionRegistry回调方法解析了
@Component @Import @ImportSource @Bean 这一系列注解的bean - 再次根据类型BeanDefinitionRegistryPostProcessor获取到beanFactory中的所有实现
- 排序 然后依次执行postProcessBeanDefinitionRegistry方法
- 解析实现了BeanFactoryPostProcessor接口的方法
- 排序 然后依次执行postProcessBeanFactory方法
【NEXT】
refresh-registerBeanPostProcessors方法解析