Spring源码系列三--invokeBeanFactoryPostProcessors

本文主要讲解AnnotationConfigApplicationContext中的refresh方法,主要讲解invokeBeanFactoryPostProcessors方法,因为这个方法才是Spring bean生命周期的开始

@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.
			//spring初始化的准备工作
			//设置启动时间,是否激活标志位等
			//初始化属性源(property source)配置
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			//获取在父类构造方法中初始化的bean工厂DefaultListableBeanFactory
			//同时设置了bean工厂的serializationId
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			prepareBeanFactory(beanFactory);

			try {
				//当前版本的spring-framework中好像没有实现
				// Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);

				//完成了扫描和parse(类 --> BeanDefinition)
				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				//注册后置处理器(通过一个list维护)
				//在调用这个方法之前DefaultListableBeanFactory已经添加了三个后置处理器
				//在prepareBeanFactory方法中添加了ApplicationContextAwareProcessor(spring会将容器的上下文环境通过set方法注入)
				//ApplicationListenerDetector()
				//在ConfigurationClassPostProcessor的postProcessBeanFactory方法中添加了ImportAwareBeanPostProcessor
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				//国际化
				initMessageSource();

				// Initialize event multicaster for this context.
				//spring事件广播器
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				onRefresh();

				// Check for listener beans and register them.
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				//调用BeanPostProcessor的实现类的方法,实例化bean放入到singleObjects中,需要validate和调用生命回调函数
				//合并BeanDefinition
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				//调用针对整个Spring容器的生命回调函数
				finishRefresh();
			}

			catch (BeansException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}

				// Destroy already created singletons to avoid dangling resources.
				destroyBeans();

				// Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}

			finally {
				// Reset common introspection caches in Spring's core, since we
				// might not ever need metadata for singleton beans anymore...
				resetCommonCaches();
			}
		}
	}

接下来我们着重研究下invokeBeanFactoryPostProcessors方法

protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
		//传入的参数是AnnotationConfigApplicationContext的父类GenericApplicationContext在构造方法中实例化的对象DefaultListableBeanFactory
		//获取自定义(我们自己定义的并且没有交给spring管理的实现类)的BeanFactoryPostProcessors
		//问题:什么时候将自定义的实现类放入到list集合中的?没有交给spring管理 怎样才能让spring获取到?
		//调用AnnotationApplicationConfigContext的addBeanFactoryPostProcessor()方法将自己的实现类传入
		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()));
		}
	}

重要的代码就是调用BeanFactory的后置处理器
在这里插入图片描述
getBeanFactoryPostProcessors()会返回一个List集合
在这里插入图片描述
在这里插入图片描述
这里存放的BeanFactoryPostProcessor是Spring通过api提供我们去扩展的时候调用add方法存入的,如果没有调用这个方法那么这里默认就是一个空的集合
在这里插入图片描述
在这里插入图片描述
可以看到其实本质就是调用List集合的add方法
进入到invokeBeanFactoryPostProcessors方法,这里面的代码就比较复杂了

/**
	 * beanFactoryPostProcessors
	 * 1.没有值的情况居多
	 * 2.有值,在外部可以通过AnnotationConfigApplicationContext的addBeanFactoryPostProcessor()方法像集合中添加元素
	 * 3.如果是通过@Bean或其他注解方式注入的后置处理器也不会在这个集合中,会在后面执行
	 */
	public static void invokeBeanFactoryPostProcessors(
			ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

		// Invoke BeanDefinitionRegistryPostProcessors first, if any.
		Set<String> processedBeans = new HashSet<>();
		//判断传入的参数beanFactory是否实现了BeanDefinitionRegistry
		//显然这里是成立的,因为传入的beanFactory是DefaultListableBeanFactory的实例(除非自己做了spring的扩展把beanFactory改了)
		if (beanFactory instanceof BeanDefinitionRegistry) {
			BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
			List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
			//BeanDefinitionRegistryPostProcessor也是继承了BeanFactoryPostProcessor
			//这里spring为什么要区分两种不同的集合?
			//BeanDefinitionRegistryPostProcessor比BeanDefinitionRegistry扩展了一个postProcessBeanDefinitionRegistry方法
			//可以获取到BeanDefinitionRegistry(这个对象的作用看了以后再来补充)
			//但是从这里可以看出我们自定义BeanFactoryPostProcessor可以通过实现BeanFactoryPostProcessor或BeanDefinitionRegistryPostProcessor两种方式实现
			List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

			//判断我们自己创建的postProcessor实现的是BeanDefinitionRegistryPostProcessor还是BeanDefinitionPostProcessor
			//BeanDefinitionRegistryPostProcessor实现类中的postProcessBeanDefinitionRegistry方法可以直接获取到BeanDefinitionRegistry(可以直接获取当前spring环境的上下文应用,比其父类更强大)
			for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
				if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
					BeanDefinitionRegistryPostProcessor registryProcessor =
							(BeanDefinitionRegistryPostProcessor) postProcessor;
					//如果实现的是BeanDefinitionRegistryPostProcessor
					//先调用实现类中的postProcessBeanDefinitionRegistry方法
					//调用这个方法的作用根据不同的实现类有不同的意义
					//但是spring默认只有一个实现ConfigurationClassPostProcessor
					registryProcessor.postProcessBeanDefinitionRegistry(registry);
					//调用完处理器内的方法后将当前的实现类添加到BeanDefinitionRegistryPostProcessor集合中
					registryProcessors.add(registryProcessor);
				}
				else {
					//如果实现的是BeanDefinitionPostProcessor
					//就直接将当前的后置处理器添加到BeanFactoryPostProcessor集合中
					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里是spring内部自己的BeanDefinitionRegistryPostProcessor实现类
			//那么spring如何获取到内部自己的实现呢?
			List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

			// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
			//spring实现了通过beanType获取到相应type的所有实现类beanName的方法
			//这个方法具体实现还没看明白,后面看了再来补充如何获取beanNames
			//PriorityOrdered和Ordered代表优先级,PriorityOrdered优先级>Ordered,所以spring在处理BeanDefinitionRegistryPostProcessor的时候是基于优先级的
			//先处理PriorityOrdered然后处理Ordered,如果同样是PriorityOrdered或Ordered则比较值,值越小优先级越高
			//getBeanNamesForType方法不是去扫描classpath目录下,而是去beanDefinitionMaps中去寻找
			//这里也会进行一次合并,并且这次合并是spring整个初始化阶段第一次合并,因为这里要调用后置处理器的方法,那么后置处理器有可能是存在父bd的
			//所以这里就需要一次合并,合并完成后会向mergedBeanDefinitions存放合并完的bd
			//但是这里的合并只会合并所有api注册的和spring内置的BeanDefinitionRegistryPostProcessor
			String[] postProcessorNames =
					beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
			//判断如果实现了PriorityOrdered接口就在这里处理,如果没有实现这个接口这里就不处理
			//ConfigurationClassPostProcessor实现了PriorityOrdered接口
			for (String ppName : postProcessorNames) {
				//PriorityOrdered接口继承了Ordered接口,但是没有扩展任何方法任何属性
				//但是如果一个类实现Ordered另一个类实现PriorityOrdered且两者的value值一样则PriorityOrdered的优先级更高
				if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
					//将当前beanName对应的BeanDefinitionRegistryPostProcessor实例加入到集合中
					//这里调用getBean方法很关键,因为在最初的构造方法中,spring只是将它初始化所需要用到的后置处理器放入到map中并没有实例化
					//所以这里调用了getBean方法,getBean方法如果获取不到实例就会去实例化
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					processedBeans.add(ppName);
				}
			}
			//以上总共有四个集合
			//1.processedBeans---存放的spring自己内部对于BeanDefinitionRegistryPostProcessor的实现类的名字(主要是判断已经执行过了就不要重复执行)
			//2.currentRegistryProcessors---也是存放spring自己内部对于BeanDefinitionRegistryPostProcessor的实现类
			//3.registryProcessors----存放所有的BeanDefinitionRegistryPostProcessor实现类以及所有内置的BeanDefinitionPostProcessor
			//4.regularPostProcessors---存放用户对于BeanDefinitionPostProcessor的实现类
			sortPostProcessors(currentRegistryProcessors, beanFactory);
			//将spring内部自己的对于BeanDefinitionRegistryPostProcessor的实现类全部都存放到registryProcessors(存放用户的实现类)集合中
			//ConfigurationClassPostProcessor是spring环境初始化时创建AnnotatedBeanDefinitionReader实时调用registerAnnotationConfigProcessorsAnnotatedBeanDefinitionReader方法加入的
			registryProcessors.addAll(currentRegistryProcessors);
			//调用了BeanDefinitionRegistryPostProcessor的扩展方法
			//这里用了委托者模式调用了currentRegistryProcessors中的实现类的扩展方法
			//但是spring初始化环境时传入的实现类其实只有一个ConfigurationClassPostProcessor
			//在bean注册进map之前调用BeanDefinitionRegistryPostProcessor实现类的方法
			//可以看出BeanDefinitionRegistryPostProcessor和BeanFactoryPostProcessor的方法调用时间有区别
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
			//至此为止所有的BeanDefinitionRegistryPostProcessor实现类的postProcessBeanDefinitionRegistry方法都已经执行完了
			//调用spring内部的BeanDefinitionRegistryPostProcessor实现类的方法后将currentRegistryProcessors清空
			currentRegistryProcessors.clear();
			// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
			postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
			for (String ppName : postProcessorNames) {
				//判断没有执行过并且实现了Order接口
				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.
			//寻找没有实现Order也没有实现PriorityOrdered接口的实现
			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;
						//将尝试值改为true,因为进入这个if说明找到了适合的后置处理器,但是在后置处理器中我们可能又添加了新的处理器(往beanDefinitionMap中put就可以 )
						//如果此时添加的BeanDefinitionRegistryPostProcessor实现类同时也实现了PriorityOrder或者Order接口也是会在这里执行的
						//所以只要能进入这个if语句块就还需要下一次的循环判断
					}
				}
				sortPostProcessors(currentRegistryProcessors, beanFactory);
				registryProcessors.addAll(currentRegistryProcessors);
				invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
				currentRegistryProcessors.clear();
			}

			// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
			//这里开始执行BeanFactoryPostProcessor的postProcessBeanFactory方法
			//registryProcessors放的是BeanDefinitionRegistryPostProcessor的实现类,但是BeanDefinitionRegistryPostProcessor实现了BeanFactoryPostProcessor所以它也有postProcessBeanFactory方法
			//registryProcessors有spring自己的实现类ConfigurationClassPostProcessor
			//ConfigurationClassPostProcessor会判断配置类是否加了@Configuration注解
			invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
			//regularPostProcessors是通过api添加的
			invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
		}

		else {
			// Invoke factory processors registered with the context instance.
			invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
		}
		//到这里位置,spring内置的BeanFactoryPostProcessor和BeanDefinitionRegistryPostProcessor以及我们通过api添加的这两种实现类和我们自定义的BeanDefinitionRegistryPostProcessor都已经执行完了
		//下面要执行的是我们通过注解等其他方式添加的(除api添加以外的)BeanFactoryPostProcessor,上面执行了ConfigurationClassPostProcessor的processConfigBeanDefinitions方法已经扫描了通过注解等方式注入的我们自己提供的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);

		// 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<>();
		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<>();
		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方法中有不同的执行时机:

  1. 遍历我们通过api提供的实现了了BeanDefinitionRegistryPostProcessorBeanFactoryPostProcessor
  2. 遍历Spring自己的实现BeanDefinitionRegistryPostProcessor接口的同时也实现了PriorityOrdered接口bean工厂后置处理器
  3. 遍历Spring自己的也包括我们提供的实现了BeanDefinitionRegistryPostProcessorOrder接口的后置处理器
  4. 遍历Spring自己的也包括我们提供的实现了BeanDefinitionRegistryPostProcessor,这个时候就不去判断是否实现了OrderPriorityOrder接口了
  5. 执行之前bean工厂后置处理器继承自父类BeanFactoryPostProcessor方法
  6. 根据BeanFactoryPostProcessor类型去BeanDefinitionMap中寻找实现该接口的实现类,然后按照实现了PriorityOrdered > Ordered > 没有实现两者接口 的顺序去执行

我们先来看下这两个接口体系的类图,然后将代码拆分讲解

在这里插入图片描述
BeanDefinitionRegistryPostProcessor继承了BeanFactoryPostProcessor,所以Spring在通过类型去遍历的时候要分两次,因为BeanFactoryPostProcessor的实现类不一定实现了BeanDefinitionRegistryPostProcessor

//这里先贴出部分代码
		Set<String> processedBeans = new HashSet<>();
		if (beanFactory instanceof BeanDefinitionRegistry) {
			BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
			List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
			List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

			
			for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
				if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
					BeanDefinitionRegistryPostProcessor registryProcessor =
							(BeanDefinitionRegistryPostProcessor) postProcessor;
				
					registryProcessor.postProcessBeanDefinitionRegistry(registry);
				
					registryProcessors.add(registryProcessor);
				}
				else {
					
					regularPostProcessors.add(postProcessor);
				}
			}

首先定义一个Set集合泛型是String类型,用来存放已经处理过的后置处理器的bean的名字,但是在当前代码中暂时用不到,在后面的处理中会用到。判断传入的beanFactory是不是实现了BeanDefinitionRegistry,这里是毋庸置疑的,因为我们传入的参数就是在AnnotationConfigApplicationContext中实例化的DefaultListableBeanFactory的实例,除非改了Spring的代码,否则这里一定是成立的。然后定义了两个集合List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();。这里只要理解了上面的UML类图就很容易理解为什么Spring需要定义两个不同的集合了,因为调用方法的时机不同。然后对beanFactoryPostProcessors集合进行遍历,这个集合的由来在上文中已经解释过了,泛型是BeanFactoryPostProcess所以集合中的元素要么实现了BeanDefinitionRegistryPostProcessor要么实现了BeanFactoryPostProcess,如果实现的是前者那么就先调用实现类的方法然后再添加到集合中,如果是后者就直接添加到regularPostProcessor集合中,此时还不会执行实现类的方法。
在这里插入图片描述
这里其实也是Spring的一个扩展点之一,在mybatis-spring最新版本的源码中就是通过BeanDefinitionRegistryPostProcessor接口做的与Spring的整合。

			List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
			String[] postProcessorNames =
					beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
				if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					processedBeans.add(ppName);
				}
			}
			sortPostProcessors(currentRegistryProcessors, beanFactory);
	registryProcessors.addAll(currentRegistryProcessors);
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
			currentRegistryProcessors.clear();

创建了一个集合,用来存放本次遍历找出来的符合要求的实现了List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();,通过类型去DefaultListableBeanFactory找出所有当前类的BeanName,但是这个getBeanNamesForType比较复杂,里面还涉及到了merge,就不放在本文中讲了,等到将Spring bean实例化的时候再来聊吧。关于BeanDefinitionRegistryPostProcessor实现类在Spring framework中只有一个ConfigurationClassPostProcessor,其中的两个方法也相当复杂,就不在本文中讲了,本文重点还是在于执行的流程。将我们找出的BeanName的集合进行遍历,判断BeanDefinition所描述的类是否实现了PriorityOrdered接口,但是这里肯定是可以找出的,就是刚才说的ConfigurationClassPostProcessor实现类,然后调用BeanFactory的getBean方法获取这个bean对的实例,放入到currentRegistryProcessors集合中,并且将beanName放入到一开始就实例化的集合processedBeans集合中
在这里插入图片描述
接下来的代码就很简单了,排序,将集合currentRegistryProcessors中的元素都存放到registryProcessors中,因为currentRegistryProcessors是个临时的集合,代表本次循环出的符合要求的后置处理器,调用完方法后会被清空掉,但是这里仅仅调用了BeanDefinitionRegistryPostProcessor的方法,所以仍然需要缓存起来。
在这里插入图片描述
调用方法,清空临时集合,接下来遍历是否还实现了Order接口的BeanDefinitionRegistryPostProcessor实现类,逻辑和上面一模一样这里就不再赘述了。下面就讲一下通过while循环遍历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();
			}

首先要说明的是,这里之所以还要再遍历一次,是因为前面我们将遍历出的结果中还判断了PriorityOrderedOrder所以,有可能存在没有实现这两个接口的实现类。定义了一个布尔值reiterate为true,姑且称为尝试值吧。第一次代码运行到这里,while循环肯定满足条件能进入,此时将尝试值改为false,然后和之前一样通过BeanDefinitionRegistryPostProcessor类型去寻找相应的bd,进入for循环遍历判断是否已经在processedBeans集合中,如果说找到一个bean不存在该集合中,说明之前没有处理过,那么就将当前bean存放到当前集合currentRegistryProcessors中,然后将BeanName放入到集合processedBeans中,将尝试值改为false,因为for循环遍历结束后就要执行刚才找到bd的方法,那么在这个方法中有可能又会新增加符合要求的bd,比如调用beanFactory.addBeanPostProcessor()方法,所以不得不叹服Spring考虑周到之处,所以只要能进入if语句块,那么就一定要再遍历一次,这就是while循环的所有逻辑,最后就调用之前遍历出来的所有实现类调用父类接口BeanFactoryPostProcessor的方法
在这里插入图片描述
最后的代码逻辑几乎一样就不再赘述了,无非是通过BeanFactoryPostProcessor类型去寻找实现类而已。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值