8.Java Spring框架源码分析-IOC-调用BeanFactoryPostProcessor的postProcess方法

目录

  • [1. 要研究的代码](#1. 要研究的代码)
  • [2. 处理BeanDefinitionRegistryPostProcessor类型的PostProcessor](#2. 处理BeanDefinitionRegistryPostProcessor类型的PostProcessor)
    • [2.1. 当前bean工厂是BeanDefinitionRegistry](#2.1. 当前bean工厂是BeanDefinitionRegistry)
      • [2.1.1. 处理传递进来的beanFactoryPostProcessors](#2.1.1. 处理传递进来的beanFactoryPostProcessors)
        • [2.1.1.1. 如果是BeanDefinitionRegistryPostProcessor类型的,调用postProcessBeanDefinitionRegistry方法](#2.1.1.1. 如果是BeanDefinitionRegistryPostProcessor类型的,调用postProcessBeanDefinitionRegistry方法)
      • [2.1.2. 处理beanFactory中其他类型BeanDefinitionRegistryPostProcessor](#2.1.2. 处理beanFactory中其他类型BeanDefinitionRegistryPostProcessor)
        • [2.1.2.1. 先调用PriorityOrdered类型的postProcessBeanDefinitionRegistry方法](#2.1.2.1. 先调用PriorityOrdered类型的postProcessBeanDefinitionRegistry方法)
        • [2.1.2.2. 然后调用Ordered类型的postProcessBeanDefinitionRegistry方法](#2.1.2.2. 然后调用Ordered类型的postProcessBeanDefinitionRegistry方法)
        • [2.1.2.3. 最后调用普通类型的postProcessBeanDefinitionRegistry方法](#2.1.2.3. 最后调用普通类型的postProcessBeanDefinitionRegistry方法)
        • [2.1.2.4. 最后同样分类调用postProcessBeanFactory方法](#2.1.2.4. 最后同样分类调用postProcessBeanFactory方法)
    • [2.2. 当前bean工厂不是BeanDefinitionRegistry](#2.2. 当前bean工厂不是BeanDefinitionRegistry)
      • [2.2.1. 只调用postProcessBeanFactory方法](#2.2.1. 只调用postProcessBeanFactory方法)
  • [3. 处理BeanFactoryPostProcessor类型的PostProcessor](#3. 处理BeanFactoryPostProcessor类型的PostProcessor)
    • [3.1. 分为PriorityOrdered、Ordered、普通三种类型](#3.1. 分为PriorityOrdered、Ordered、普通三种类型)
    • [3.2. 调用其postProcessBeanFactory方法](#3.2. 调用其postProcessBeanFactory方法)

1. 要研究的代码

  • AbstractApplicationContext invokeBeanFactoryPostProcessors
java 复制代码
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
	//把当前bean工厂,所有的bean工厂PostProcessors传入
	PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());

	// Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
	// (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
	if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
		beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
		beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
	}
}

关键的是这一句PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());,他会把当前bean工厂,所有的bean工厂PostProcessors传入调用PostProcessorRegistrationDelegate的 invokeBeanFactoryPostProcessors方法

  • PostProcessorRegistrationDelegate invokeBeanFactoryPostProcessors
java 复制代码
public static void invokeBeanFactoryPostProcessors(
		ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

	// Invoke BeanDefinitionRegistryPostProcessors first, if any.
	Set<String> processedBeans = new HashSet<String>();

	//当前bean工厂是BeanDefinitionRegistry【√】
	if (beanFactory instanceof BeanDefinitionRegistry) {
		BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
		List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<BeanFactoryPostProcessor>();
		List<BeanDefinitionRegistryPostProcessor> registryProcessors = new LinkedList<BeanDefinitionRegistryPostProcessor>();

		//遍历传递进来的beanFactoryPostProcessors
		for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
		    //如果是BeanDefinitionRegistryPostProcessor类型的
			if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
				BeanDefinitionRegistryPostProcessor registryProcessor =
						(BeanDefinitionRegistryPostProcessor) postProcessor;
				//那么调用其他postProcessBeanDefinitionRegistry方法
				registryProcessor.postProcessBeanDefinitionRegistry(registry);
				//加入registryProcessors
				registryProcessors.add(registryProcessor);
			}
			//不是的话加入regularPostProcessors
			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<BeanDefinitionRegistryPostProcessor>();

		// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
		//从bean工厂中获取所有类型为BeanDefinitionRegistryPostProcessor的所有PostProcessor的名字
		String[] postProcessorNames =
				beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
		//遍历这些PostProcessor,如果是PriorityOrdered类型的,从bean工厂中获取实例并放入currentRegistryProcessors中保存起来
		for (String ppName : postProcessorNames) {
			if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
				currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
				processedBeans.add(ppName);
			}
		}
		//排序、调用postProcessBeanDefinitionRegistry方法、清空集合	
		sortPostProcessors(currentRegistryProcessors, beanFactory);
		registryProcessors.addAll(currentRegistryProcessors);
		invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
		currentRegistryProcessors.clear();

		// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
		//遍历这些PostProcessor,如果是Ordered类型的,从bean工厂中获取实例并放入currentRegistryProcessors中保存起来
		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);
			}
		}
		//排序、调用postProcessBeanDefinitionRegistry方法、清空集合	
		sortPostProcessors(currentRegistryProcessors, beanFactory);
		registryProcessors.addAll(currentRegistryProcessors);
		invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
		currentRegistryProcessors.clear();

		// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
		//遍历这些PostProcessor,如果是普通类型的,从bean工厂中获取实例并放入currentRegistryProcessors中保存起来
		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;
				}
			}
			//排序、调用postProcessBeanDefinitionRegistry方法、清空集合	
			sortPostProcessors(currentRegistryProcessors, beanFactory);
			registryProcessors.addAll(currentRegistryProcessors);
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
			currentRegistryProcessors.clear();
		}

		// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
		//下面两句依次调用所有Processor(PriorityOrdered、Ordered、普通)的postProcessBeanFactory方法(不是postProcessBeanDefinitionRegistry方法)
		invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
		invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
	}
	//当前bean工厂不是BeanDefinitionRegistry
	else {
		// Invoke factory processors registered with the context instance.
		//只调用postProcessBeanFactory方法
		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!
	//从bean工厂中获取所有类型为BeanFactoryPostProcessor的所有PostProcessor的名字
	String[] postProcessorNames =
			beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

	// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
	// Ordered, and the rest.
	List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
	List<String> orderedPostProcessorNames = new ArrayList<String>();
	List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
	//遍历这些PostProcessor,分为PriorityOrdered、Ordered、普通的加入三个集合
	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.
	//排序、调用PriorityOrdered的BeanFactoryPostProcessor的postProcessBeanFactory方法
	sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
	invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

	// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
	//排序、调用Ordered的BeanFactoryPostProcessor的postProcessBeanFactory方法
	List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
	for (String postProcessorName : orderedPostProcessorNames) {
		orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
	}
	sortPostProcessors(orderedPostProcessors, beanFactory);
	invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

	// Finally, invoke all other BeanFactoryPostProcessors.
	//排序、调用普通的BeanFactoryPostProcessor的postProcessBeanFactory方法
	List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
	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();
}

2. 处理BeanDefinitionRegistryPostProcessor类型的PostProcessor

2.1. 当前bean工厂是BeanDefinitionRegistry

java 复制代码
//当前bean工厂是BeanDefinitionRegistry【√】
if (beanFactory instanceof BeanDefinitionRegistry) {
//...
}

2.1.1. 处理传递进来的beanFactoryPostProcessors

java 复制代码
//遍历传递进来的beanFactoryPostProcessors
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
   //...
}
2.1.1.1. 如果是BeanDefinitionRegistryPostProcessor类型的,调用postProcessBeanDefinitionRegistry方法
java 复制代码
//如果是BeanDefinitionRegistryPostProcessor类型的
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
    BeanDefinitionRegistryPostProcessor registryProcessor =
            (BeanDefinitionRegistryPostProcessor) postProcessor;
    //那么调用其他postProcessBeanDefinitionRegistry方法
    registryProcessor.postProcessBeanDefinitionRegistry(registry);
    //加入registryProcessors
    registryProcessors.add(registryProcessor);
}
//不是的话加入regularPostProcessors
else {
    regularPostProcessors.add(postProcessor);
}

2.1.2. 处理beanFactory中其他类型BeanDefinitionRegistryPostProcessor

java 复制代码
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>();

// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
//从bean工厂中获取所有类型为BeanDefinitionRegistryPostProcessor的所有PostProcessor的名字
String[] postProcessorNames =
        beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
2.1.2.1. 先调用PriorityOrdered类型的postProcessBeanDefinitionRegistry方法
java 复制代码
//遍历这些PostProcessor,如果是PriorityOrdered类型的,从bean工厂中获取实例并放入currentRegistryProcessors中保存起来
for (String ppName : postProcessorNames) {
    if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
        currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
        processedBeans.add(ppName);
    }
}
//排序、调用postProcessBeanDefinitionRegistry方法、清空集合	
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
2.1.2.2. 然后调用Ordered类型的postProcessBeanDefinitionRegistry方法
java 复制代码
// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
//遍历这些PostProcessor,如果是Ordered类型的,从bean工厂中获取实例并放入currentRegistryProcessors中保存起来
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);
    }
}

//排序、调用postProcessBeanDefinitionRegistry方法、清空集合	
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
2.1.2.3. 最后调用普通类型的postProcessBeanDefinitionRegistry方法
java 复制代码
// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
//遍历这些PostProcessor,如果是普通类型的,从bean工厂中获取实例并放入currentRegistryProcessors中保存起来
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;
        }
    }
    //排序、调用postProcessBeanDefinitionRegistry方法、清空集合	
    sortPostProcessors(currentRegistryProcessors, beanFactory);
    registryProcessors.addAll(currentRegistryProcessors);
    invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
    currentRegistryProcessors.clear();
}
2.1.2.4. 最后同样分类调用postProcessBeanFactory方法
java 复制代码
// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
//下面两句依次调用所有Processor(PriorityOrdered、Ordered、普通)的postProcessBeanFactory方法(不是postProcessBeanDefinitionRegistry方法)
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);

2.2. 当前bean工厂不是BeanDefinitionRegistry

java 复制代码
//当前bean工厂不是BeanDefinitionRegistry
else {
    // Invoke factory processors registered with the context instance.
    //只调用postProcessBeanFactory方法
    invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
}

2.2.1. 只调用postProcessBeanFactory方法

3. 处理BeanFactoryPostProcessor类型的PostProcessor

java 复制代码
//从bean工厂中获取所有类型为BeanFactoryPostProcessor的所有PostProcessor的名字
String[] postProcessorNames =
        beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

3.1. 分为PriorityOrdered、Ordered、普通三种类型

java 复制代码
// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
List<String> orderedPostProcessorNames = new ArrayList<String>();
List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
//遍历这些PostProcessor,分为PriorityOrdered、Ordered、普通的加入三个集合
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);
    }
}
 

3.2. 调用其postProcessBeanFactory方法

java 复制代码
// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
//排序、调用PriorityOrdered的BeanFactoryPostProcessor的postProcessBeanFactory方法
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
//排序、调用Ordered的BeanFactoryPostProcessor的postProcessBeanFactory方法
List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
for (String postProcessorName : orderedPostProcessorNames) {
    orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
sortPostProcessors(orderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

// Finally, invoke all other BeanFactoryPostProcessors.
//排序、调用普通的BeanFactoryPostProcessor的postProcessBeanFactory方法
List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
for (String postProcessorName : nonOrderedPostProcessorNames) {
    nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);