我们继续跟进PostProcessorRegistrationDelegate的invokeBeanFactoryPostProcessors方法(比较长):
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
Set<String> processedBeans = new HashSet<>();
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
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.
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);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
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);
currentRegistryProcessors.clear();
// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
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();
}
// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
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!
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) {
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.
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();
}
在代码中首先判断beanFactory是不是BeanDefinitionRegistry的对象,我们这里beanFactory类型是DefaultListableBeanFactory对象,它是BeanDefinitionRegistry对象,进入if代码块;
if代码块添加了regularPostProcessors和registryProcessors两个集合。
程序遍历了传入的beanFactoryPostProcessors对象(就是上一篇博客最后提到的长度为3的List),如果当前遍历到的beanFactoryPostProcessor是BeanDefinitionRegistryPostProcessor类型的话,就调用它的postProcessBeanDefinitionRegistry方法,然后将它添加到registryProcessors集合当中;否则,就会加入到regularPostProcessors集合当中。
接下来又构造了BeanDefinitionRegistryPostProcessor类的一个currentRegistryProcessors集合,先获取所有的BeanDefinitionRegistryPostProcessor类型的对象,遍历这些对象,如果实现了PriorityOrdered接口的话,就将它添加到集合currentRegistryProcessors中,同事也加入到函数一开始创建的一个set中:processedBeans,代表处理过了。
接下来对currentRegistryProcessors集合做一个排序,将currentRegistryProcessors集合添加到registryProcessors集合当中,invokeBeanDefinitionRegistryPostProcessors会让所有的currentRegistryProcessors集合中的对象调用postProcessBeanDefinitionRegistry方法,然后清空currentRegistryProcessors集合。
接下来,又会获得beanFactory中所有BeanDefinitionRegistryPostProcessor的实现,遍历,判断是否处理过,且实现了Ordered接口,如果没有处理过,且实现了Ordered接口,就加入currentRegistryProcessors集合中,并加入processedBeans中。后面又会对currentRegistryProcessors集合做一个排序,添加到egularPostProcessors集合当中,并且遍历调用postProcessBeanDefinitionRegistry方法,再次清空currentRegistryProcessors集合。
后面的while循环,又是一个重复的事情。又会获得beanFactory中所有BeanDefinitionRegistryPostProcessor的实现,遍历,调用,清空,直到没有未处理过的实现。
为什么重复?因为BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry方法,可能又添加了一个BeanDefinitionRegistryPostProcessor接口的实现。
while循环结束后,regularPostProcessors和registryProcessors两个集合的多有对象的postProcessBeanFactory方法会被触发。
接下来,获得所有的BeanFactoryPostProcessor实例(postProcessorNames),构建三个集合:priorityOrderedPostProcessors、orderedPostProcessorNames、nonOrderedPostProcessorNames。遍历postProcessorNames,判断是否处理过,处理过则不再处理。否则,判断是否实现了接口PriorityOrdered,实现了,就加入priorityOrderedPostProcessors集合;再否则,判断是否实现了Ordered接口,如果实现了,加入到orderedPostProcessorNames中,再否则,加入到nonOrderedPostProcessorNames中。
priorityOrderedPostProcessors、orderedPostProcessorNames、nonOrderedPostProcessorNames三个集合会依次处理。先对priorityOrderedPostProcessors排序,对其内部的实例调用postProcessBeanFactory方法;对orderedPostProcessorNames排序,对其内部的实例调用postProcessBeanFactory方法;对nonOrderedPostProcessorNames内部的实例调用postProcessBeanFactory方法。
最后,清空一下上述步骤产生的缓存。
invokeBeanFactoryPostProcessors方法结束后,回到AbstractApplicationContext类型的invokeBeanFactoryPostProcessors方法执行一个逻辑判断。
if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
这里由于没有包含loadTimeWeaver,因此跳过执行。
refresh方法的invokeBeanFactoryPostProcessors方法到此结束了。
接下来要执行后面的registerBeanPostProcessors方法。