invokeBeanFactoryPostProcessors

本文详细解析了Spring框架中BeanFactoryPostProcessor和BeanDefinitionRegistryPostProcessor的工作流程,包括它们的分类、执行顺序和作用。特别关注了如何处理用户自定义的处理器,以及Spring内部的处理器是如何被发现和执行的。
/** 1.对用户自定义的BeanFactoryPostProcessor和BeanDefinitionRegistryPostProcessor做分类。放到两个list。

	 简称 BeanFactoryPostProcessor=》A    BeanDefinitionRegistryPostProcessor=》B

	 并且执行BeanDefinitionRegistryPostProcessor => postProcessBeanDefinitionRegistry

	 没执行的是:

	 【BeanFactoryPostProcessor的postProcessBeanFactory】



	 2.拿到beanFactory中的BeanDefinitionRegistryPostProcessor&&实现了PriorityOrdered接口的bean

	 排序 => 执行postProcessBeanDefinitionRegistry

	 加入B



	 3.拿出beanFactory中的BeanDefinitionRegistryPostProcessor&&实现了Ordered接口的bean

	 排序 => postProcessBeanDefinitionRegistry

	 加入B



	 4.拿出beanFactory中的BeanDefinitionRegistryPostProcessor,既没有实现PriorityOrdered,也没有Ordered。普通的BeanDefinitionRegistryPostProcessor类,用一个while把剩下的全部执行完。

	 加入B



	 5.执行A和B的postProcessBeanFactory

	 【BeanDefinitionRegistryPostProcessor的postProcessBeanFactory】



	 6.取出bean工厂的所有beanFactoryPostProcessor

	 分成三类= 》PriorityOrdered、Ordered、普通的FactoryPostProcessor

	 分别执行,beanFactoryPostProcessor的postProcessBeanFactory



	 7.清除缓存。

	 【BeanDefinitionRegistryPostProcessor的postProcessBeanFactory】
	 *
	 * @param beanFactory bean工厂
	 * @param beanFactoryPostProcessors  实现了BeanFactoryPostProcessor的类,未加@component,并且被我们手动add到spring中
		List<BeanFactoryPostProcessor> beanFactoryPostProcessors => 对这个list做分类
	 */
	public static void invokeBeanFactoryPostProcessors(
			ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
		// Invoke BeanDefinitionRegistryPostProcessors first, if any.
		Set<String> processedBeans = new HashSet<>();

		/** 整个beanFactory的关系非常复杂,实际上spring的beanFactory默认的实现是DefaultListableBeanFactory
			而DefaultListableBeanFactory又继承了一堆的方法,实现了一系列的接口,其中就有一个叫做BeanDefinitionRegistry */
		if (beanFactory instanceof BeanDefinitionRegistry) {
			// 强转成接口类型
			BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

			// BeanDefinitionRegistryPostProcessor继承了BeanFactoryPostProcessor
			List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<>();// 父类
			List<BeanDefinitionRegistryPostProcessor> registryProcessors = new LinkedList<>();// 子类

			/** 两个list再去循环做不同的逻辑处理,因为子类和父类的接口不一样(子类比父类多了一个方法),所以需要分开,做不同的处理
			 *  这个for实际上就是对beanFactoryPostProcessors的内容去做归类,
			 *  如果是BeanFactoryPostProcessor => regularPostProcessors
			 *  如果是BeanDefinitionRegistryPostProcessor => registryProcessors  */
			for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
				if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
					// 强转一下
					BeanDefinitionRegistryPostProcessor registryProcessor =
							(BeanDefinitionRegistryPostProcessor) postProcessor;
					// 进行处理,调用接口的实现方法
					registryProcessor.postProcessBeanDefinitionRegistry(registry);
					// 归类 => 把当前循环的实体,放入list中
					registryProcessors.add(registryProcessor);
				}
				// 如果是BeanFactoryPostProcessor => 不处理,放入list
				else {
					regularPostProcessors.add(postProcessor);
				}
			}
			/**
			 * 在这里对用户自定义的BeanFactoryPostProcessor或者BeanDefinitionRegistryPostProcessor进行归类
			 * 同时会处理好BeanDefinitionRegistryPostProcessor的实现方法 => postProcessBeanDefinitionRegistry
			 */








			// 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<>();
			/**	org.springframework.context.annotation.AnnotatedBeanDefinitionReader#AnnotatedBeanDefinitionReader()
  					org.springframework.context.annotation.AnnotationConfigUtils#registerAnnotationConfigProcessors()
						 // 当前环境 = registey,这里会检查当起的beanFactory是否包含这个类
						 if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
							 // 显然这里一开始会没有这个,所以会进来这里
							 // 直接new出一个BeanDefintion,就是相当于把一个class类转成一个BeanDefinition
							 RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
							 def.setSource(source);
							 // beanDefs这个玩意不知道有啥用,只是知道,在这个方法中,会把对应的bean和beanName转成一个handler,然后add到这个里面
							 beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
						 }
			 */
			// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
			/** 这里做了一个反向操作,根据Class(BeanDefinitionRegistryPostProcessor)的类型,从bean工厂里面拿出这个beanDefiniti
			 *  对应dename => 因为AnnotatedBeanDefinitionReader实例化的时候会把ConfigurationClassPostProcessor实例化之后
			 	放进beanfactory中,所以这里必定可以得到一个beanName数组(最起码也会有一个ConfigurationClassPostProcessor)  */
			String[] postProcessorNames =
					beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
			for (String ppName : postProcessorNames) {
				// 拿出这个bean,同时这个bean实现了PriorityOrdered接口
				if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
					// 通过beanName"正向"获取到beanDefinition,currentRegistryProcessors=>放到list当中
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					processedBeans.add(ppName);// 把beanName放到set当中,可能是为了去重?
				}
			}
			// 对spring自己的BeanFactoryPostProcessor进行排序
			sortPostProcessors(currentRegistryProcessors, beanFactory);
			// 进行合并,把currentRegistryProcessors合并到registryProcessors中,后面统一对registryProcessors循环做处理即可
			registryProcessors.addAll(currentRegistryProcessors);
			/** 这里会调用BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry方法来操作
			 // registryProcessors和currentRegistryProcessors,其中registryProcessors已经在上面被调用过了
			 // 这里的registryProcessors是合并了currentRegistryProcessors,而currentRegistryProcessors还没执行过postProcessBeanDefinitionRegistry
			 // 这里执行一下 => currentRegistryProcessors => 是spring自己实现 */
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
			// 搞定,清空掉,准备后面重用
			currentRegistryProcessors.clear();






			// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
			// 再拿一遍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);
			registryProcessors.addAll(currentRegistryProcessors);
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
			currentRegistryProcessors.clear();




			// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
			// 调用剩下的所有实现了BeanDefinitionRegistryPostProcessor接口的类(赶尽杀绝,绝不放过任一个)
			boolean reiterate = true;
			while (reiterate) {
				reiterate = false;
				postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
				for (String ppName : postProcessorNames) {
					// 既没有实现Ordered也没有实现PriorityOrdered接口的BeanDefinitionRegistryPostProcessor类
					// 这里用processedBeans来控制,如果执行过了,不会再执行
					if (!processedBeans.contains(ppName)) {
						currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
						processedBeans.add(ppName);
						// 只要能进到这里,说明有新的beanDefinitionRegistryPostProcessor需要执行
						// 执行新的BeanDefinitionRegistryPostProcessor,就可能会产生新的BeanDefinitionRegistryPostProcessor
						// 所以这里搞成true
						reiterate = true;
					}
				}
				sortPostProcessors(currentRegistryProcessors, beanFactory);
				registryProcessors.addAll(currentRegistryProcessors);
				invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
				currentRegistryProcessors.clear();
			}
			/** 这个while能把所有的BeanDefinitionRegistryPostProcessor执行完毕  */


			// 执行postProcessBeanFactory,此方法是BeanDefinitionRegistryPostProcessor接口的一个方法
			invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);

			// 调用 => List<BeanFactoryPostProcessor> regularPostProcessors
			invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
		}
		else {
			// Invoke factory processors registered with the context instance.
			invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
		}

		/** 到了这里,实际上只是执行了,用户自定义的BeanFactoryPostProcessor和BeanDefinitionRegistryPostProcessor
		 *  执行了spring的bean工厂中的BeanDefinitionRegistryPostProcessor接口的两个方法
		 *  还差spring的bean工厂的BeanFactoryPostProcessor还没执行*/


		/** 对spring中的BeanFactoryPostProcessor做分类
		 * 实现PriorityOrdered做一类,Ordered做一类,剩下的做一类
		 * 分别对应 priorityOrderedPostProcessors,orderedPostProcessorNames,nonOrderedPostProcessorNames */
		// 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);
			}
		}

		// 执行实现了PriorityOrdered接口的beanFactoryPostPorcessor
		// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
		sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
		invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);


		// 执行实现了Ordered接口的beanFactoryPostProcessor
		// 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);
		invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);



		// 执行普通的beanFactoryPorstProcessor
		// Finally, invoke all other BeanFactoryPostProcessors.
		List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
		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();
	}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值