PostProcessorRegistrationDelegate

概述

PostProcessorRegistrationDelegateAbstractApplicationContext委托执行post processors任务的工具类
这里的postProcessor包括两类 :

  • BeanFactoryPostProcessor
  • BeanPostProcessor

实际上BeanFactoryPostProcessor又细分为两类:

1.BeanDefinitionRegistryPostProcessorBeanDefinitionRegistry后置处理器

2.BeanFactoryPostProcessorBeanFactory后置处理器

BeanDefinitionRegistryPostProcessor其实继承自BeanFactoryPostProcessor,是一种特殊的BeanFactoryPostProcessorBeanDefinitionRegistryPostProcessor的设计目的是在常规BeanFactoryPostProcessor处理BeanFactory(也就是容器)前先对bean注册做处理,比如注册更多的bean,实现此目的是通过BeanDefinitionRegistryPostProcessor定义的方法postProcessBeanDefinitionRegistry

如果一个实现类是BeanDefinitionRegistryPostProcessor,那么它的postProcessBeanDefinitionRegistry方法总是要早与它的postProcessBeanFactory方法被调用。

该工具类位于包

package org.springframework.context.support;

两个主要功能

功能一 : 执行 BeanFactoryPostProcessor

背景介绍 :

  1. ApplicationContext对象在构造函数执行时会创建一些BeanFactoryPostProcessor,比如
    AnnotationConfigEmbeddedWebApplicationContext构造函数中最终会通过
    AnnotationConfigUtils注册进来一些BeanFactoryPostProcessor/BeanPostProcessor

  2. ApplicationContextApplicationContextInitializer被执行时会创建特定功能的
    BeanFactoryPostProcessor记录在ApplicationContext中(注意:这里不是注册到容器中,
    而是记录为ApplicationContext的属性);

  3. ApplicationContext.refresh()中,BeanFactory preparepost process 之后,会
    调用 invokeBeanFactoryPostProcessors() 执行这些BeanFactoryPostProcessor
    完成指定的功能。

举例来看,一个缺省的Web SpringBootApplication 会被添加这几个 BeanFactoryPostProcessor

ConfigurationWarningsApplicationContextInitializer$ConfigurationWarningsPostProcessor

SharedMetadataReaderFactoryContextInitializer$CachingMetadataReaderFactoryPostProcessor

ConfigFileApplicationListener$PropertySourceOrderingPostProcessor

invokeBeanFactoryPostProcessors()的具体实现逻辑如下 :

/**
 * 调用BeanFactoryPostProcessor
 * 
 * @参数 beanFactory 应用上下文的 BeanFactory 实例
 * @参数 beanFactoryPostProcessors 应用上下文指定要执行的 BeanFactoryPostProcessor
**/
public static void invokeBeanFactoryPostProcessors(
		ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

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

	if (beanFactory instanceof BeanDefinitionRegistry) {
		BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
		
		// 用于记录常规 BeanFactoryPostProcessor
		List<BeanFactoryPostProcessor> regularPostProcessors = 
			new LinkedList<BeanFactoryPostProcessor>();
			
		// 用于记录 BeanDefinitionRegistryPostProcessor
		List<BeanDefinitionRegistryPostProcessor> registryProcessors = 
			new LinkedList<BeanDefinitionRegistryPostProcessor>();
		
		// 遍历所有参数传递进来的 BeanFactoryPostProcessor(它们并没有作为bean注册在容器中)
        // 将所有参数传入的 BeanFactoryPostProcessor 分成两组 : 
        // BeanDefinitionRegistryPostProcessor 和常规 BeanFactoryPostProcessor
        // 1.如果是BeanDefinitionRegistryPostProcessor,现在执行postProcessBeanDefinitionRegistry(),
        // 2.否则记录为一个常规 BeanFactoryPostProcessor,现在不执行处理
		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.
		// currentRegistryProcessors 用于记录当前正要被执行的BeanDefinitionRegistryPostProcessor
		List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = 
			new ArrayList<BeanDefinitionRegistryPostProcessor>();

		// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
		// 1. 对 Bean形式 BeanDefinitionRegistryPostProcessor + PriorityOrdered 的调用
		// 找出所有容器中注册为bean存在的BeanDefinitionRegistryPostProcessor
		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);
		// Bean形式存在的 BeanDefinitionRegistryPostProcessor 也添加到 registryProcessors 中
		registryProcessors.addAll(currentRegistryProcessors);
				
		// 对bean形式存在的 BeanDefinitionRegistryPostProcessor 执行其对		
		// BeanDefinitionRegistry的postProcessBeanDefinitionRegistry()
		invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
		currentRegistryProcessors.clear();//清空

		// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
		// 2. 对 Bean形式 BeanDefinitionRegistryPostProcessor + 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);
		// Bean形式存在的 BeanDefinitionRegistryPostProcessor 也添加到 registryProcessors 中
		registryProcessors.addAll(currentRegistryProcessors);
		// 对Bean形式存在的 BeanDefinitionRegistryPostProcessor 执行其对		
		// BeanDefinitionRegistry的postProcessBeanDefinitionRegistry()		
		invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
		currentRegistryProcessors.clear();//清空

		// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
		// 3. 对 Bean形式 BeanDefinitionRegistryPostProcessor , 并且未实现
		// PriorityOrdered或者Ordered接口进行处理,直到没有未被处理的
		boolean reiterate = true;
		while (reiterate) {
			reiterate = false;
			postProcessorNames = beanFactory.getBeanNamesForType(
					BeanDefinitionRegistryPostProcessor.class, true, false);
			for (String ppName : postProcessorNames) {
				if (!processedBeans.contains(ppName)) {
					// 发现了一个尚未被处理的 BeanDefinitionRegistryPostProcessor 
					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.
		// 因为BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProcessor,所以这里
		// 也对所有 BeanDefinitionRegistryPostProcessor 调用其方法 postProcessBeanFactory()
		invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
		// 对所有常规 BeanFactoryPostProcessor 调用其方法 postProcessBeanFactory()
		invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
	}

	else {
		// Invoke factory processors registered with the context instance.
		invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
	}

	// 以上逻辑执行了所有参数传入的和以bean定义方式存在的BeanDefinitionRegistryPostProcessor,
	// 也执行了所有参数传入的BeanFactoryPostProcessor, 但是尚未处理所有以bean定义方式存在的
	// BeanFactoryPostProcessor, 下面的逻辑处理这部分 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);

	// 将所有目前记录的所有BeanFactoryPostProcessor分成三部分 :
	// 1. 实现了 PriorityOrdered 接口的,
	// 2. 实现了 Ordered 接口的,
	// 3. 其他.
	// 接下来的逻辑会对这三种BeanFactoryPostProcessor分别处理
	// 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>();
	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.
	// 1. 执行Bean形式 BeanFactoryPostProcessor + PriorityOrdered
	sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
	invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

	// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
	// 2. 执行Bean形式 BeanFactoryPostProcessor + Ordered
	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.
	// 3. 执行Bean形式 BeanFactoryPostProcessor , 没有实现 PriorityOrdered 或者 Ordered 接口
	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();
}

功能二 : 注册 BeanPostProcessor

public static void registerBeanPostProcessors(
		ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {

	// 找到所有注册到容器的 BeanPostProcessor 的名字
	String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

	// Register BeanPostProcessorChecker that logs an info message when
	// a bean is created during BeanPostProcessor instantiation, i.e. when
	// a bean is not eligible for getting processed by all BeanPostProcessors.
	int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
	beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

	// Separate between BeanPostProcessors that implement PriorityOrdered,
	// Ordered, and the rest.
	// 将 BeanPostProcessor 分成三类处理 : PriorityOrdered, Ordered 和 其它。
	List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
	List<BeanPostProcessor> internalPostProcessors = new ArrayList<BeanPostProcessor>();
	List<String> orderedPostProcessorNames = new ArrayList<String>();
	List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
	for (String ppName : postProcessorNames) {
		if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
			BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
			priorityOrderedPostProcessors.add(pp);
			if (pp instanceof MergedBeanDefinitionPostProcessor) {
				internalPostProcessors.add(pp);
			}
		}
		else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
			orderedPostProcessorNames.add(ppName);
		}
		else {
			nonOrderedPostProcessorNames.add(ppName);
		}
	}

	// First, register the BeanPostProcessors that implement PriorityOrdered.
	sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
	registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

	// Next, register the BeanPostProcessors that implement Ordered.
	List<BeanPostProcessor> orderedPostProcessors = new ArrayList<BeanPostProcessor>();
	for (String ppName : orderedPostProcessorNames) {
		BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
		orderedPostProcessors.add(pp);
		if (pp instanceof MergedBeanDefinitionPostProcessor) {
			internalPostProcessors.add(pp);
		}
	}
	sortPostProcessors(orderedPostProcessors, beanFactory);
	registerBeanPostProcessors(beanFactory, orderedPostProcessors);

	// Now, register all regular BeanPostProcessors.
	List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
	for (String ppName : nonOrderedPostProcessorNames) {
		BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
		nonOrderedPostProcessors.add(pp);
		if (pp instanceof MergedBeanDefinitionPostProcessor) {
			internalPostProcessors.add(pp);
		}
	}
	registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

	// Finally, re-register all internal BeanPostProcessors.
	sortPostProcessors(internalPostProcessors, beanFactory);
	registerBeanPostProcessors(beanFactory, internalPostProcessors);

	// Re-register post-processor for detecting inner beans as ApplicationListeners,
	// moving it to the end of the processor chain (for picking up proxies etc).
	// 重新注册用于检测实现了ApplicationListener接口的内部bean的post processor,
	// 把它放到processor链的尾部
	beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}

  
  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值