10-Spring源码解析之refresh(4)——invokeBeanFactoryPostProcessors(1)

上一篇:9-Spring源码解析之refresh(3)——【prepareBeanFactory】与【postProcessBeanFactory】

上篇文章讲的是refresh方法中调用的第三个方法prepareBeanFactory和第四个方法postProcessBeanFactory,
本篇文章我们来接着介绍refresh的第五个方法,也是非常重要的方法:invokeBeanFactoryPostProcessors(beanFactory);

1. 【AbstractApplicationContext】# invokeBeanFactoryPostProcessors

根据方法的名称,我们可以看出该方法的功能是执行BeanFactory的后置处理器。那么我们了解一下BeanFactoryPostProcessor的结构,对它有一个全局的认识。
在这里插入图片描述
我们可以看到BeanFactoryPostProcessor是一个接口,下面又很多实现类,还有一个子接口:BeanDefinitionRegistryPostProcessor

接下来我们具体看一下Spring是如何帮我们实现这个功能的。
refresh方法中调用AbstractApplicationContext # invokeBeanFactoryPostProcessors方法

	protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
		// 【1】调用PostProcessorRegistrationDelegate类的invokeBeanFactoryPostProcessors方法
		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()));
		}
	}

从上面代码标注【1】的部分是实现该方法的核心,我们可以看到该方法是通过调用PostProcessorRegistrationDelegate类的invokeBeanFactoryPostProcessors方法实现的。注意这个类PostProcessorRegistrationDelegate,这个方法是执行BeanFactory的后置处理器,在后面的文章中可以看到在执行Bean的后置处理器的时候还会接触和使用到该类。

【1】处首先调用了当前类的getBeanFactoryPostProcessors方法,然后将方法返回值作为参数传入到PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors中。

	private final List<BeanFactoryPostProcessor> beanFactoryPostProcessors = new ArrayList<>();
	public List<BeanFactoryPostProcessor> getBeanFactoryPostProcessors() {
		return this.beanFactoryPostProcessors;
	}

从上一篇文章2.节可以知道,本项目没有重写postProcessorBeanFactory方法,因此这里获取到的beanFactoryPostProcessors实际上是空值,因此传入到PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors中的第二个参数值为空ArrayList。第一个参数值为我们之前获取到的beanFactory。为了防止忘记,我再贴一下当前的beanFactory中包含哪些beanDefinition

在这里插入图片描述
上述图片中可以看出有6个beanDefinition,这6个beanDefinition实际上分两类:

  • 第一类:内置的beanDefinition
    • 前五个为内置的beanDefinition,它们是Spring给我们内置的bean后置处理器
  • 第二类:项目中的配置类
    • 最后一个mainConfig_AOP为和Bean定义有关的beanDefinition,它是我们项目中的配置类。

另外,注意,这几个内置(internal)处理器的类型:详情参见:4-Spring容器创建之this()(1)——AnnotatedBeanDefinitionReader中的3.4节

  • internalConfigurationAnnotationProcessor的类型为:ConfigurationClassPostProcessor
  • internalAutowiredAnnotationProcessor的类型为:AutowiredAnnotationBeanPostProcessor
  • internalCommonAnnotationProcessor的类型为:CommonAnnotationBeanPostProcessor
  • internalEventListenerProcessor的类型为:EventListenerMethodProcessor
  • internalEventListenerFactory的类型为:DefaultEventListenerFactory

下面我们进入到PostProcessorRegistrationDelegate类的invokeBeanFactoryPostProcessors方法中看一下它究竟是如何实现的。

2. 【PostProcessorRegistrationDelegate】# invokeBeanFactoryPostProcessors

这个方法的方法体很长,需要有十足的耐心才能读完。而且该方法不只做了一件事情,而是做了三件事情。

	public static void invokeBeanFactoryPostProcessors(
			ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

		// Invoke BeanDefinitionRegistryPostProcessors first, if any.
		Set<String> processedBeans = new HashSet<>();
//---------------------------------------------【功能一】--------------------------------------------------
		// -----【1】----- 2.1 介绍
		// 这个if方法判断结果为true,具体原因在2.1 详细介绍
		if (beanFactory instanceof BeanDefinitionRegistry) {
			BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
			// regularPostProcessors:装载普通的BeanFactoryPostProcessor
			// registryProcessors: 装载和Bean定义有关的 BeanDefinitionRegistryPostProcessor
			List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
			List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

			// 在1.节 我们知道 beanFactoryPostProcessors 传过来的是空的ArrayList, 所以不走for循环
			for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
				if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
					BeanDefinitionRegistryPostProcessor registryProcessor =
							(BeanDefinitionRegistryPostProcessor) postProcessor;
					registryProcessor.postProcessBeanDefinitionRegistry(registry);
					registryProcessors.add(registryProcessor);
				}
				else {
					regularPostProcessors.add(postProcessor);
				}
			}
//---------------------------------------------【功能二】--------------------------------------------------
			// 下面就是开始执行Spring容器里面的PostProcessor
			// 执行顺序:先执行实现了 PriorityOrdered 接口的, 再执行实现了 Ordered 接口的, 最后执行剩下的
			List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

			// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
			// 下面按照类型获取 beanFactory 中的后置处理器
			// 首先获取 BeanDefinitionRegistryPostProcessor 类型的后置处理器名字。
			// 从1. 节可以看出当前 beanFactory 中属于BeanDefinitionRegistryPostProcessor类型的后置处理器为 internalConfigurationAnnotationProcessor
			String[] postProcessorNames =
					beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
			for (String ppName : postProcessorNames) {
				if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
					// 执行到这里想要获取 beanFactory 中名字为ppName的Bean,
					// 但是此时Spring中只有BeanDefinition,而没有真正的Bean, 所以获取不到会开始创建名字为 ppName 的Bean.
					// 实际上在这里创建的 Bean 为 ConfigurationClassPostProcessor 类型的
					// 该类型为处理@Configuration注解的类
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					processedBeans.add(ppName);
				}
			}
			// 排序
			sortPostProcessors(currentRegistryProcessors, beanFactory);
			registryProcessors.addAll(currentRegistryProcessors);
			// 是把currentRegistryProcessors里面所有的处理器一个一个获取出来,然后执行每个处理器对应的postProcessBeanDefinitionRegistry,这个方法非常重要。
			// 根据上面的代码可以知道属于BeanDefinitionRegistryPostProcessor类型的只有ConfigurationClassPostProcessor
			// 因此invokeBeanDefinitionRegistryPostProcessors里面执行的是ConfigurationClassPostProcessor的postProcessBeanDefinitionRegistry方法
			// -----【2】-----在下一篇文章中详细介绍这个非常重要的方法
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
			currentRegistryProcessors.clear();

			// Next, invoke the BeanDefinitionRegistryPostProcessors that implement 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.
			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();
			}
//---------------------------------------------【功能三】--------------------------------------------------
			// 执行BeanDefinitionRegistryPostProcessor的postProcessBeanFactory方法,
			// 实际上是使用cgLib将配置类增强
			// -----【3】-----这个方法也很重要,在下一篇文章中会详细讲解。
			// 注意此处调用的是invokeBeanFactoryPostProcessors
			// 上面【2】处调用的是 invokeBeanDefinitionRegistryPostProcessors
			invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
			invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
		}

		else {
			// 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!
		// 执行BeanFactoryPostProcessor 
		// 执行顺序:先执行实现了 PriorityOrdered 接口的, 再执行实现了 Ordered 接口的, 最后执行剩下的
		// 下面的顺序与功能三的顺序一样,只是功能三做的是invokeBeanDefinitionRegistryPostProcessors 功能四做的是 invokeBeanFactoryPostProcessors
		// 这里获取到Spring中两个BeanFactoryPostProcessor:internalEventListenerProcessor 和 internalConfigurationAnnotationProcessor
		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);
			}
		}

		// First, invoke the BeanFactoryPostProcessors that implement 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));
		}
		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));
		}
		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();
	}

以上,我将invokeBeanFactoryPostProcessors人为的分为四个功能,下面分别讲一下每个功能所作的事情:

  • 【功能一】

    • 执行beanFactoryPostProcessorspostProcessBeanDefinitionRegistry。但是在1.节中也介绍过了,因为当前项目中没有传入BeanFactory,因此这里的for循环不会执行。
  • 【功能二】 创建ConfigurationClassPostProcessor并解析@Configuration注解

    • 首先获取当前beanFactory中所有 BeanDefinitionRegistryPostProcessors类型的BeanDefinition,在1节中已经给出当前beanFactory只有7个BeanDefinition,而属于BeanDefinitionRegistryPostProcessors类型的只有一个:internalConfigurationAnnotationProcessor
    • 执行BeanDefinitionRegistryPostProcessors类型的BeanpostProcessBeanDefinitionRegistry 。按照顺序执行(PriorityOrdered->Ordered -> 剩下的)
    • 这里需要先从beanFactory中获取BeanDefinitionRegistryPostProcessors类型的Bean,但是此时还没有,所以在这里Spring第一次创建了Bean,这个Bean的类型就是ConfigurationClassPostProcessor,这个Bean就是用来解析@Configuration注解的。
    • 注册完Bean后,开始执行这个BeanpostProcessBeanDefinitionRegistry方法,也就是上述标注【2】的部分,这个方法非常重要,实现的功能就是利用注册的ConfigurationClassPostProcessor来解析项目中的配置类,将配置类中的所有注解类都转换为beanDefinition,然后存储在beanFactory中。下篇文章具体讲解解析的过程。
  • 【功能三】

    • 执行ConfigurationClassPostProcessorpostProcessBeanFactory方法,目的是使用CGLIB增强我们的配置类。但是这里面还没有将beanFactory中的配置类替换为增强后的配置类。
  • 【功能四】

    • 执行BeanFactoryPostProcessor类型的BeanpostProcessBeanFactory。注意区分功能二中执行的是:BeanDefinitionRegistryPostProcessors类型的BeanpostProcessBeanDefinitionRegistry

2.1. beanFactory instanceof BeanDefinitionRegistry

2.节【1】处,if (beanFactory instanceof BeanDefinitionRegistry)判断条件结果为true,主要的原因如下:
我们传入的beanFactory是在refresh方法调用的第二个方法ConfigurableListableBeanFactory获取到的,而获取到的beanFactory类型实际上是DefaultListablebleBeanFactory类型,那么为什么这个类型的beanFactotry对象属于BeanDefinitionRegistry类型呢,主要是因为DefaultListableBeanFactory类的实现关系。

下面给出DefaultListableBeanFactory类的代码可以很明显的看到该类不仅实现了ConfigurableListableBeanFactory接口还实现了BeanDefinitionRegistry接口,所以自然上述if条件返回true

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
		implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable

2.2. invokeBeanFactoryPostProcessors功能解惑

实际上在Spring中,这个invokeBeanFactoryPostProcessors方法更类似于一个模板方法,它拥有四个功能,其中第二个功能和第四个功能很有意思,都是调用BeanPostProcessor类型的方法,但是调用的方法是不同的,先在【功能二】中调用postProcessBeanDefinitionRegistry,后在【功能四】中调用postProcessBeanFactory。为什么会这样呢?

实际上从名字就可以看出来,postProcessBeanDefinitionRegistry是用来创建BeanDefinition的,而postProcessBeanFactory是用来处理BeanFactory的。另外postProcessBeanFactory是可以修改BeanDefinition的。为了确保在修改BeanDefinition之前所有的BeanDefinition都存在,所以要先执行postProcessBeanDefinitionRegistry后执行postProcessBeanFactory

3. 总结

invokeBeanFactoryPostProcessors的功能

  • 执行beanFactoryPostProcessorspostProcessBeanDefinitionRegistry
  • 创建ConfigurationClassPostProcessor并解析@Configuration注解
  • 执行BeanFactoryPostProcessor类型的BeanpostProcessBeanFactory

注意以上是将BeanDefinition都已经注册到了beanFactory中,还没有开始创建Bean,但是通过上述分析,我们知道,其实也创建了两个Spring的内部使用的BeanConfigurationClassPostProcessorEventListenerMethodProcessor

下一篇文章会具体讲解两个问题:

  • invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);PriorityOrdered

    • ConfigurationClassPostProcessor类如何解析@Configuration注解类
  • invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);

    • ConfigurationClassPostProcessor类如何增强配置类
发布了397 篇原创文章 · 获赞 71 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/xiaojie_570/article/details/104637562