invokeBeanFactoryPostProcessors是容器刷新的第五步,其作用就是创建已注册的BeanFactoryPostProceessor并执行他们的postProcess处理方法。这一过程的核心方法就是PostProcessorRegistrationDelegate类的静态方法invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors)
下面上源码,其中英文注释是源码中的,中文注释是我对每一步作用的解释
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
//为什么要先处理BeanDefinitionRegistryPostProcessors呢?
//因为BeanDefinitionRegistryPostProcessors可能会向beanfactory中注册新的BeanFactoryPostProcessor,如果后处理BeanDefinitionRegistryPostProcessors,那么新注册的就无法被创建了
Set<String> processedBeans = new HashSet<>(); //记录所有已经处理过的BeanFactoryPostProcessor的名字
if (beanFactory instanceof BeanDefinitionRegistry) { //beanfactory是BeanDefinitionRegistry类型
//无论是XML配置还是注解配置,其内部的beanfactory默认都是DefaultListableBeanFactory也就是BeanDefinitionRegistry
//但ClassPathXmlApplicationContext本身不是BeanDefinitionRegistry
//而AnnotationConfigApplicationContext本身是BeanDefinitionRegistry
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>(); //普通的BeanFactoryPostProcessor
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>(); //BeanDefinitionRegistryPostProcessor
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors /*方法参数传进来的*/) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) { //如果是BeanDefinitionRegistryPostProcessor
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
registryProcessor.postProcessBeanDefinitionRegistry(registry); //直接执行postProcessBeanDefinitionRegistry方法
registryProcessors.add(registryProcessor); //存入registryProcessors的list
}
else { //如果是普通的BeanFactoryPostProcessor
regularPostProcessors.add(postProcessor); //存入regularPostProcessors的list
}
}
//以上过程会执行传入的所有BeanFactoryPostProcessor中是BeanDefinitionRegistryPostProcessor类型的postProcessBeanDefinitionRegistry方法
//还会把传入的所有BeanFactoryPostProcessor按照类型分为registryProcessors或regularPostProcessors
//这一部分是处理传入方法的List<BeanFactoryPostProcessor> ,接下来才处理beanfactory中其他的BeanFactoryPostProcessor
//而这些BeanFactoryPostProcessor此时还没有实例化,只是以BeanDefinition的形式存在
// 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中查找bean定义信息,其中类型是BeanDefinitionRegistryPostProcessor的bean名字会被记录
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) { //找出优先排序的
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); //创建bean实例并记录
processedBeans.add(ppName); //记录
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory); //排序
registryProcessors.addAll(currentRegistryProcessors); //记录
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); //执行他们的postProcessBeanDefinitionRegistry方法
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)); //创建bean实例并记录
processedBeans.add(ppName); //记录
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory); //排序
registryProcessors.addAll(currentRegistryProcessors); //记录
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); //执行他们的postProcessBeanDefinitionRegistry方法
currentRegistryProcessors.clear();
//以上步骤创建了实现排序接口的BeanDefinitionRegistryPostProcessor,并执行了postProcessBeanDefinitionRegistry方法
// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
boolean reiterate = true;
while (reiterate) {
reiterate = false;
//重新查找所有BeanDefinitionRegistryPostProcessor的定义信息
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName)) { //找出之前步骤中还没有处理的
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); //创建bean实例并记录
processedBeans.add(ppName); //记录
reiterate = true; //只要新产生了bean实例,就要重新扫描beanfactory
//因为下面的invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
//方法可能会使beanfactory中产生新的BeanDefinition,如果这个BeanDefinition是BeanDefinitionRegistryPostProcessor类型
//那么只有重新扫描才能发现并生成对应实例
//由以上步骤看,如果新加入的bean定义中也实现了排序接口,则排序接口不一定产生预期效果,优先级低的可能已经处理完了
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory); //排序
registryProcessors.addAll(currentRegistryProcessors); //记录
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); //执行他们的postProcessBeanDefinitionRegistry方法
currentRegistryProcessors.clear();
}
// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory); //执行registryProcessors的postProcessBeanFactory方法
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory); //执行regularPostProcessors的postProcessBeanFactory方法
}
else { //如果beanfactory不是bean定义注册中心
// Invoke factory processors registered with the context instance.
invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
}
//以上步骤处理了BeanDefinitionRegistratyPostProcessor
//下面处理是BeanFactoryPostProcessor且不是BeanDefinitionRegistratyPostProcessor类型的
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let the bean factory post-processors apply to them!
//到beanfactory中查找所有BeanFactoryPostProcessor的名字
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
//前面处理BeanDefinitionRegistratyPostProcessor时已经处理过的在这跳过
}
else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
//实现优先排序的直接创建bean
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();
}
对方法执行的解释已经全部在注释中了,如有不对的欢迎指出。