AbstractApplicationContext.refresh
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// 准备刷新 Prepare this context for refreshing.
prepareRefresh(); //下面一句 当然,这里说的 Bean 还没有初始化,只是配置信息都提取出来了
................................
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory); // 这里入口
..........................
}
处理手动注册的beanFactoryPostProcessors
context.addBeanFactoryPostProcessor(myBeanDefinitionRegistryPostProcessor);
Set<String> processedBeans = new HashSet<>();
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>(); // 正常的BeanFactoryPostProcessor,非BeanDefinitionRegistryPostProcessor类型的BeanFactoryPostProcessor
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>(); // 注册的BeanFactoryPostProcessor,BeanDefinitionRegistryPostProcessor类型的BeanFactoryPostProcessor
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) { // 手动注册列表
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);
}
else {
regularPostProcessors.add(postProcessor);
}
}
}
先判断是否是BeanDefinitionRegistry,如果是,则得到BeanFactory内部得到beanFactoryPostProcessors,然后遍历,
判断是否是BeanDefinitionRegistryPostProcessor,如果是,则执行postProcessBeanDefinitionRegistry,并把实例加到registryProcessors列表里,如果不是,则把实例放到regularPostProcessors列表里
处理Spring内部得到实现了PriorityOrdered接口的BeanDefinitionRegistryPostProcessor
// PriorityOrdered, Ordered, and the rest.
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
String[] postProcessorNames = // 从Spring容器中得到所有的BeanDefinitionRegistryPostProcessor
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) { // 第1个Processors:这里找到key:org.springframework.context.annotation.internalConfigurationAnnotationProcessor value: ConfigurationClassPostProcessor
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { // 优先取PriorityOrdered
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); //spring找到ConfigurationClassPostProcessor并实例化
processedBeans.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory); // 排序
registryProcessors.addAll(currentRegistryProcessors); // 把currentRegistryProcessors添加到registryProcessors
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
- 从Spring容器得到所有的BeanDefinitionRegistryPostProcessor实现类,然后遍历
- 如果该实现类实现了PriorityOrdered,把实例放到currentRegistryProcessors列表,同时把beanName放到processedBeans
- 重新排序(BeanDefinitionRegistryPostProcessor所有实现PriorityOrdered的实例)
- 把currentRegistryProcessors结果集添加到registryProcessors
- 循环遍历currentRegistryProcessors,调用postProcessBeanDefinitionRegistry方法
- currentRegistryProcessors清空
处理Spring内部得到实现了Ordered接口的BeanDefinitionRegistryPostProcessor
// 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)) { // 上面未处理过,并且是实现接口Ordered
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
- 从Spring容器得到所有的BeanDefinitionRegistryPostProcessor实现类,然后遍历
- 如果该实现类实现了Ordered,把实例放到currentRegistryProcessors列表,同时把beanName放到processedBeans
- 重新排序(BeanDefinitionRegistryPostProcessor所有实现Ordered的实例)
- 把currentRegistryProcessors结果集添加到registryProcessors
- 循环遍历currentRegistryProcessors,调用postProcessBeanDefinitionRegistry方法
- currentRegistryProcessors清空
// 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();
}
- 从Spring容器得到所有的BeanDefinitionRegistryPostProcessor实现类,然后遍历
- 这里处理的是没有实现PriorityOrdered和Ordered接口的类,把实例放到currentRegistryProcessors列表,同时把beanName放到processedBeans
- 重新排序
- 把currentRegistryProcessors结果集添加到registryProcessors
- 循环遍历currentRegistryProcessors,调用postProcessBeanDefinitionRegistry方法
- currentRegistryProcessors清空
这之前都是处理BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry
下面开始处理BeanFactoryPostProcessor的postProcessBeanFactory
// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory); // 调用BeanFactoryPostProcessor的postProcessBeanFactory
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory); // 调用BeanFactoryPostProcessor的postProcessBeanFactory
把BeanFactoryPostProcessor放到不同的容器
// 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);
}
}
- 从Spring容器得到所有的BeanFactoryPostProcessor实现类,然后遍历
- 上面已经处理过的,则直接忽略
- 实现PriorityOrdered接口的,则添加到priorityOrderedPostProcessors列表
- 实现Ordered接口的,则添加到orderedPostProcessorNames列表
- 没有Ordered接口,则添加到nonOrderedPostProcessorNames列表
处理priorityOrderedPostProcessors
// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
sortPostProcessors(priorityOrderedPostProcessors, beanFactory); // 排序 这里都是PriorityOrdered接口的BeanFactoryPostProcessor
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory); // 调用具体BeanFactoryPostProcessor的postProcessBeanFactory
- 先处理PriorityOrdered接口,对priorityOrderedPostProcessors排序
- 执行BeanFactoryPostProcessor的postProcessBeanFactory
处理orderedPostProcessors
// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>();
for (String postProcessorName : orderedPostProcessorNames) {
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
sortPostProcessors(orderedPostProcessors, beanFactory); // 排序 这里都是Ordered接口的BeanFactoryPostProcessor
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory); // 调用具体BeanFactoryPostProcessor的postProcessBeanFactory
处理nonOrderedPostProcessors
// Finally, invoke all other BeanFactoryPostProcessors. 没有实现Ordered、PriorityOrdered接口的BeanFactoryPostProcessor列表
List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
for (String postProcessorName : nonOrderedPostProcessorNames) {
nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory); // 调用具体BeanFactoryPostProcessor的postProcessBeanFactory
优先级
第一步
- 先处理手动注册的BeanDefinitionRegistryPostProcessor,执行postProcessBeanDefinitionRegistry方法并添加到registryProcessors队列
- 手动注册的如果有BeanFactoryPostProcessor,先添加到regularPostProcessors
第2步
同级别先排序,标记已处理,添加到currentRegistryProcessors队列,然后排序,添加到registryProcessors队列,循环currentRegistryProcessors,调用postProcessBeanDefinitionRegistry,处理完成清空currentRegistryProcessors队列
- 从Spring容器得到BeanDefinitionRegistryPostProcessor
- PriorityOrdered > Ordered > 没有Ordered
- 数字越小优先级越高
第3步
- invokeBeanFactoryPostProcessors(registryProcessors, beanFactory); // 上面所有的BeanDefinitionRegistryPostProcessor
- invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory); //手动注册的BeanFactoryPostProcessor
第4步
获取所有当前Spring容器所有的BeanFactoryProcessor
已处理过的忽略,把PriorityOrdered放到
priorityOrderedPostProcessors (PriorityOrdered)
orderedPostProcessorNames (Ordered)
nonOrderedPostProcessorNames (没有PriorityOrdered和Ordered接口)
每个容器都先排序,然后执行postProcessBeanFactory (PriorityOrdered > Ordered > 没有Ordered)