spring 源码解读四 invokeBeanFactoryPostProcessors(beanFactory) 方法

这一章重点看下 AbstractApplicationContext  -> invokeBeanFactoryPostProcessors(beanFactory) 这个方法

AbstractApplicationContext  -> invokeBeanFactoryPostProcessors(beanFactory)

这个方法算是spring的灵魂方法之一了,在说这个方法之前有必要画下spring实例化单例bean的流程图

如图所示,这次我们要看的这个方法就是执行beanFactoryPostProcessor接口的相关实现类,beanFactoryPostProcessor是一个接口,它还有一个子接口

beanDefinitionRegisterPostProcessor,因此定义在配置文件中的或者在解析xml过程中产生的 beanFactoryPostProcessor或者通过自定义beanFactory工厂添加

进去的 beanFactoryPostProcessor 的子类都会在invokeBeanFactoryPostProcessors(beanFactory) 中被执行到

它还有一个非常重要的子类ConfigurationClassProcessor 这个类是Spring 实现各种注解解析的基础以及是spring boot 实现自动装配的基础

这里面解析的注解包括 @Component  @ComponentScan  @ PropertySource  @Import  @ ImportSource  @Bean  

更详细的说明在下一节,这一节先说 invokeBeanFactoryPostProcessors(beanFactory),废话不多说,直接上代码

/**
	 * Instantiate and invoke all registered BeanFactoryPostProcessor beans,
	 * respecting explicit order if given.
	 * <p>Must be called before singleton instantiation.
	 */
	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<BeanFactoryPostProcessor> beanFactoryPostProcessors)
public static void invokeBeanFactoryPostProcessors(
			ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

		// Invoke BeanDefinitionRegistryPostProcessors first, if any.

		//首先定义一个容器将已经解析过的 BeanFactoryPostProcessor 缓存起来
		Set<String> processedBeans = new HashSet<>();

		//这里先判断 beanFactory 是否是一个BeanDefinitionRegistry 即实现的是  BeanDefinitionRegistryPostProcessor 这个接口
		//如果是一个 BeanDefinitionRegistry,则在处理的时候可能又新增了新的 BeanFactoryPostProcessor,需要循环处理
		if (beanFactory instanceof BeanDefinitionRegistry) {

			BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

			// 此集合用处理 BeanFactoryPostProcessor 的 postProcessBeanFactory 方法
			List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();

			// 此集合用处理 BeanDefinitionRegistryPostProcessor 的 postProcessBeanFactory 方法
			List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

			/*这里遍历的是用户在调
				invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors)
				这个方法的时候 传入的 beanFactoryPostProcessors 这个集合。。。 要注意下
			*/
			for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
				if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
					BeanDefinitionRegistryPostProcessor registryProcessor =
							(BeanDefinitionRegistryPostProcessor) postProcessor;

					//这里直接把 BeanDefinitionRegistryPostProcessor 的 postProcessBeanDefinitionRegistry 方法处理了
					registryProcessor.postProcessBeanDefinitionRegistry(registry);

					// 把 BeanDefinitionRegistryPostProcessor 的 postProcessBeanFactory 方法加到集合里面最后一起处理
					registryProcessors.add(registryProcessor);
				}
				else {
					// 把 BeanFactoryPostProcessor 的 postProcessBeanFactory 方法加到集合里面最后一起处理
					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 这个接口的集合
			List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

			// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.

			//从配置文件或注解中扫描出所有实现了BeanDefinitionRegistryPostProcessor接口的类
			String[] postProcessorNames =
					beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
			for (String ppName : postProcessorNames) {
				if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
					//将从 从配置文件或注解中扫描出来的实现了 BeanDefinitionRegistryPostProcessor 这个接口 并实现了PriorityOrdered 这个接口 的类 实例化并存入 currentRegistryProcessors
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					//将已经处理过得类加入缓存,防止重复处理
					processedBeans.add(ppName);
				}
			}
			//因为实现了 PriorityOrdered 接口因此需要排下序
			sortPostProcessors(currentRegistryProcessors, beanFactory);

			// 把 BeanDefinitionRegistryPostProcessor 的 postProcessBeanFactory 方法加到集合里面最后一起处理
			registryProcessors.addAll(currentRegistryProcessors);

			//这里也是直接把 BeanDefinitionRegistryPostProcessor 的 postProcessBeanDefinitionRegistry 方法处理了
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);

			//因为上面已经把 currentRegistryProcessors 加入到了 registryProcessors 因此这里可以重新使用currentRegistryProcessors,把它清空了,下面再用它
			currentRegistryProcessors.clear();

			// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
			//从配置文件或注解中扫描出所有实现了BeanDefinitionRegistryPostProcessor接口的类
			postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);

			for (String ppName : postProcessorNames) {
				//将从 从配置文件或注解中扫描出来的实现了 BeanDefinitionRegistryPostProcessor 这个接口 并实现了Ordered 这个接口 的类 实例化并存入 currentRegistryProcessors
				if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					//将已经处理过得类加入缓存,防止重复处理
					processedBeans.add(ppName);
				}
			}
			//因为实现了 Ordered 接口因此需要排下序
			sortPostProcessors(currentRegistryProcessors, beanFactory);

			// 把 BeanDefinitionRegistryPostProcessor 的 postProcessBeanFactory 方法加到集合里面最后一起处理
			registryProcessors.addAll(currentRegistryProcessors);

			//这里也是直接把 BeanDefinitionRegistryPostProcessor 的 postProcessBeanDefinitionRegistry 方法处理了
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);

			//因为上面已经把 currentRegistryProcessors 加入到了 registryProcessors 因此这里可以重新使用currentRegistryProcessors,把它清空了,下面再用它
			currentRegistryProcessors.clear();


			// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
			//这里即为在处理上面的 BeanDefinitionRegistryPostProcessor 过程中如果产生了新的实现了 BeanDefinitionRegistryPostProcessor 的接口那么需要循环处理

			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;
					}
				}
				sortPostProcessors(currentRegistryProcessors, beanFactory);
				registryProcessors.addAll(currentRegistryProcessors);
				invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
				currentRegistryProcessors.clear();
			}

			// Now, invoke the postProcessBeanFactory callback of all processors handled so far.

			//最后把所有实现了 BeanDefinitionRegistryPostProcessor 接口的 postProcessBeanFactory 方法都执行了
			invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);

			//最后再把只实现了 BeanFactoryPostProcessor 接口的 postProcessBeanFactory 方法执行了
			invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
		}

		else {
			// Invoke factory processors registered with the context instance.
			//这里如果一个接口没有实现 BeanDefinitionRegistry 则直接把它的 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!

		//下面的流程就是处理只实现了 BeanFactoryPostProcessor 的接口的类
		String[] postProcessorNames =
				beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

		// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
		// Ordered, and the rest.

		// 实现了BeanFactoryPostProcessor 且实现了 PriorityOrdered 的集合
		List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();

		// 实现了实现了BeanFactoryPostProcessor 且实现了 Ordered 的集合
		List<String> orderedPostProcessorNames = new ArrayList<>();

		// 实现了BeanFactoryPostProcessor 但是没有实现任何排序接口的集合
		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);
			}
		}

		// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
		//排序并先执行 实现了BeanFactoryPostProcessor 且实现了 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));
		}
		//排序然后执行 实现了BeanFactoryPostProcessor 且实现了 Ordered 的集合
		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));
		}
		//最后执行实现了BeanFactoryPostProcessor但是没有实现任何排序接口的 集合
		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();
	}

这里面的代码太多了,相关注释就都写到代码里了

AbstractApplicationContext  -> invokeBeanFactoryPostProcessors(beanFactory) 这个方法的解析也就到此为止了,下一节将解析ConfigurationClassPostProcessor

请大家关注下博客谢谢

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值