spring源码解析——PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(初始化beanFactory后置处理器)

刚开始看源码,可能文笔不太好,有些地方可能理解错了,欢迎大佬指点

protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
		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(ConfigurableListableBeanFactory beanFactory, List beanFactoryPostProcessors) 详解
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
        //存放bean后置处理器名称的集合
		Set<String> processedBeans = new HashSet<>();

        //判断传入的beanFactory是否属于BeanDefinitionRegistry类型,如果属于则进入处理,不属于直接执行方法
		if (beanFactory instanceof BeanDefinitionRegistry) {
		    //由于上面已经判断了传入的beanFactory属于BeanDefinitionRegistry类型,此处可以直接强转而不报错
			BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
			//创建bean后置处理器的集合<BeanFactoryPostProcessor>变量
			List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
			//创建bean后置处理器的集合<BeanDefinitionRegistryPostProcessor>变量
			List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
            //BeanFactoryPostProcessor与BeanDefinitionRegistryPostProcessor的区别请看下文叙述
            
            //循环遍历传入的bean后置处理器
			for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
			    //如果该bean后置处理器的类型为BeanDefinitionRegistryPostProcessor
				if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
				    //该bean后置处理器强转类型BeanDefinitionRegistryPostProcessor
					BeanDefinitionRegistryPostProcessor registryProcessor =
							(BeanDefinitionRegistryPostProcessor) postProcessor;
					//集合<BeanDefinitionRegistryPostProcessor>变量添加此bean后置处理器
					registryProcessor.postProcessBeanDefinitionRegistry(registry);
					//集合<BeanFactoryPostProcessor>变量添加此工厂后置处理器
					registryProcessors.add(registryProcessor);
				}
				//否则
				else {
				    //只在集合<BeanFactoryPostProcessor>变量添加此bean后置处理器
					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.
			//创建集合<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors变量(着重创建),后续用户执行后置处理器的 方法
			List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

			// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
			//把类型为BeanDefinitionRegistryPostProcessor的bean后置处理器的类型转为类型名并创建后置装载处理器名称的数组postProcessorNames
			String[] postProcessorNames =
					beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
			//遍历每一个bean后置处理器
			for (String ppName : postProcessorNames) {
			    //如果该bean后置处理器排序优先级为最高级(PriorityOrdered为最高优先级,关于bean后置处理器优先级在下文叙述)
				if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
				    //根据类型名转为bean后置处理器BeanDefinitionRegistryPostProcessor存入List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					//上面的装载bean后置处理器的集合添加此bean后置处理器名字
					processedBeans.add(ppName);
				}
			}
			//排序currentRegistryProcessors
			sortPostProcessors(currentRegistryProcessors, beanFactory);
			//<BeanDefinitionRegistryPostProcessor> registryProcessors变量添加currentRegistryProcessors里面所有的元素
			registryProcessors.addAll(currentRegistryProcessors);
			//执行registryProcessors里面所有后续处理器的方法
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
			//清除currentRegistryProcessors所有元素(只删除元素不删除对象本身,集合对象长度为0且非null)
			currentRegistryProcessors.clear();

			// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
			//把所有BeanDefinitionRegistryPostProcessor的bean后置处理器的类型名提取出来存入postProcessorNames
			postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
			//遍历所有的postProcessorNames数组元素
			for (String ppName : postProcessorNames) {
			    //如果该bean后置处理器不在processedBeans变量中且该后置处理器的排序优先级为Ordered(第二优先级),则
				if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
				    //根据类型名转为bean后置处理器BeanDefinitionRegistryPostProcessor存入List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					//processedBeans添加此后置处理器的名称
					processedBeans.add(ppName);
				}
			}
			//currentRegistryProcessors排序
			sortPostProcessors(currentRegistryProcessors, beanFactory);
			//registryProcessors添加currentRegistryProcessors排序所有的元素
			registryProcessors.addAll(currentRegistryProcessors);
			//执行currentRegistryProcessors后置处理器集合变量排序里面所有的对应的方法
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
			清除currentRegistryProcessors所有元素(只删除元素不删除对象本身,集合对象长度为0且非null)
			currentRegistryProcessors.clear();

            //接下来就处理优先级最低的
			// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
			//创建一个阈值结束死循环
			boolean reiterate = true;
			//创建一个死循环
			while (reiterate) {
			    //先把阈值定义为false
				reiterate = false;
				//把所有BeanDefinitionRegistryPostProcessor的bean后置处理器的类型名提取出来存入postProcessorNames
				postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
				//循环postProcessorNames数组
				for (String ppName : postProcessorNames) {
				    //如果此类型名没出现载processedBeans中,则
					if (!processedBeans.contains(ppName)) {
					    //根据类型名转为bean后置处理器BeanDefinitionRegistryPostProcessor存入<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors	currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					    //processedBeans添加此后置处理器的名称
						processedBeans.add(ppName);
						//阈值reiterate=true,继续循环下去
						reiterate = true;
					}
				}
				//排序sortPostProcessors
				sortPostProcessors(currentRegistryProcessors, beanFactory);
				//<BeanDefinitionRegistryPostProcessor> registryProcessors(bean后置处理器集合)放入currentRegistryProcessors的所有元素
				registryProcessors.addAll(currentRegistryProcessors);
				//调用currentRegistryProcessors中所有后置处理器的方法
				invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
				//清除currentRegistryProcessors所有元素(只删除元素不删除对象本身,集合对象长度为0且非null)
				currentRegistryProcessors.clear();
			}

			// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
			//执行registryProcessors内的所有元素(后置处理器)方法
			invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
			//执行regularPostProcessors内的所有元素(后置处理器)的方法
			invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
		}

		else {
		    //直接执行bean后置处理器方法
			// 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!
		//把bean后置处理器的类型为BeanFactoryPostProcessor的所有类型名转为字符串并创建postProcessorNames数组装载
		String[] postProcessorNames =
				beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

		// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
		// Ordered, and the rest.
		//剩下的代码执行上面那一堆都没能执行到的bean后置处理器
		//首先定义一个优先级最高的后置处理器集合变量
		List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
		//再定义一个优先级第二的后置处理器变量
		List<String> orderedPostProcessorNames = new ArrayList<>();
		//最后定义一个优先级最后的后置处理器变量
		List<String> nonOrderedPostProcessorNames = new ArrayList<>();
		//遍历postProcessorNames
		for (String ppName : postProcessorNames) {
		    //如果该bean后置处理器名已包含在processedBeans里,则什么都不处理
			if (processedBeans.contains(ppName)) {
				// skip - already processed in first phase above
			}
			//如果该bean后置处理器属于最高优先级则把其放入priorityOrderedPostProcessors
			else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
				priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
			}
			//如果该bean后置处理器属于第二优先级则把其放入orderedPostProcessorNames
			else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
				orderedPostProcessorNames.add(ppName);
			}
			//都不是的则放入nonOrderedPostProcessorNames
			else {
				nonOrderedPostProcessorNames.add(ppName);
			}
		}
        
        
		// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
		//首先执行优先级最高的bean后置处理器
		//排序priorityOrderedPostProcessors
		sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
		//执行priorityOrderedPostProcessors集合的方法
		invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

		// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
		//然后执行优先级第二的bean后置处理器
		//先定义一个集合变量装载bean后置处理器<BeanFactoryPostProcessor> orderedPostProcessors,长度为orderedPostProcessorNames的长度
		List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
		//循环遍历orderedPostProcessorNames(第二优先级的bean后置处理器名数组)
		for (String postProcessorName : orderedPostProcessorNames) {
		    //根据处理器名字反射出该处理器的对象并放入orderedPostProcessors
			orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
		}
		//排序orderedPostProcessors
		sortPostProcessors(orderedPostProcessors, beanFactory);
		//执行orderedPostProcessors元素内的bean后续处理器的方法
		invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

		// Finally, invoke all other BeanFactoryPostProcessors.
		//最后,执行最次优先级的bean后置处理器
		//定义一个集合变量装载bean后置处理器<BeanFactoryPostProcessor> nonOrderedPostProcessors,其长度=nonOrderedPostProcessorNames(最次优先级的bean后置处理器名的数组)长度
		List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
		//遍历nonOrderedPostProcessorNames
		for (String postProcessorName : nonOrderedPostProcessorNames) {
		    //根据bean后置处理器名称反射出后置处理器对象并放入nonOrderedPostProcessors
			nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
		}
		//不排序,直接执行nonOrderedPostProcessors内的每个元素(后置处理器)的方法
		invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);

		// Clear cached merged bean definitions since the post-processors might have
		// modified the original metadata, e.g. replacing placeholders in values...
		//清除bean——因为已经修改了其元数据
		beanFactory.clearMetadataCache();
BeanDefinitionRegistryPostProcessor与BeanFactoryPostProcessor区别

BeanDefinitionRegistryPostProcessor 侧重于创建自定义的bd 而 BeanFactoryPostProcessor侧重于对已有bd属性的修改。
BeanDefinitionRegistryPostProcessor 先于 BeanFactoryPostProcessor 执行

bean后置处理器执行优先级

PriorityOrdered————>Ordered————>nonOrdered

流程归纳

先判断传入的beanFactory是否BeanDefinitionRegistry类型,如果不是就直接注册bean工厂后置处理器,如果是则按照优先级分别进行插入缓存、排序、注册、清除缓存。然后再循环出所有的bean工厂后置处理器,与上面一样按照优先级进行插入缓存、排序、注册、清除缓存,期间排除已经注册的beanFactory后置处理器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值