在上一讲中,我们详细地分析了一下BeanFactory的创建以及预准备工作的流程。紧接上一讲,我们就要来看看接下来又做了哪些工作。
现在,程序已经运行到了下面这行代码处了。
可以看到这儿会执行一个叫invokeBeanFactoryPostProcessors的方法,这个方法我们之前也看过,它就是来执行BeanFactoryPostProcessor的。
BeanFactoryPostProcessor就是BeanFactory的后置处理器。那么,它是什么时候来执行的呢?我们不妨看一下它的源码,如下图所示。
从它内部方法的描述上来看,BeanFactoryPostProcessor(也可以说它里面的那个方法)是在BeanFactory标准初始化之后执行的。
而BeanFactory标准初始化正是我们上一讲所阐述的内容。
我们之前也看过BeanFactoryPostProcessor接口的继承树,如下图所示。
可以看到,BeanFactoryPostProcessor接口下还有一个子接口,即
BeanDefinitionRegistryPostProcessor。 以前,我们还用过BeanDefinitionRegistryPostProcessor这个接口给IOC容器中额外添加过组件,不知你还记不记得?
接下来,我们就来看看invokeBeanFactoryPostProcessors这个方法里面到底做了哪些事,也就是看一下BeanFactoryPostProcessor的整个执行过程
其实,当你看完这篇文章之后,你就知道了在invokeBeanFactoryPostProcessors方法里面主要就是执行了BeanFactoryPostProcessors#postProcessBeanFactory方法。
@FunctionalInterfacepublic interface BeanFactoryPostProcessor {void postProcessBeanFactory(ConfigurableListableBeanFactory var1) throws BeansException;}
或者BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry和postProcessBeanFactory这俩方法,
//注意这里是接口继承,不是接口实现public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry var1) throws BeansException;}
tips:先执行BeanDefinitionRegistryPostProcessor的方法
我们可以按下F5快捷键进入invokeBeanFactoryPostProcessors方法里面去瞧一瞧,如下图所示,可以看到现在程序来到了如下这行代码处。
invokeBeanFactoryPostProcessors大方法
于是,按下F7快捷键退出getBeanFactoryPostProcessors方法,返回到调用层,然后按下F5快捷键进入invokeBeanFactoryPostProcessors方法里面去一探究竟,如下图所示,是不是来到了我们熟悉的地方啊😊
其中,一开始的注释就告诉了我们,无论什么时候都会先调用实现了BeanDefinitionRegistryPostProcessor接口的类。
大家一定要注意哟!紧接着会先来判断我们这个beanFactory是不是BeanDefinitionRegistry。之前我们在上一讲中就已经说过了,我们生成的BeanFactory对象是DefaultListableBeanFactory类型的,而且还使用了ConfigurableListableBeanFactory接口进行接收。这里我们就来看下DefaultListableBeanFactory类是不是实现了BeanDefinitionRegistry接口:
我们这个beanFactory,AnnotationConfigApplicationContext,是不是BeanDefinitionRegistry?
首先我们生成的BeanFactory对象是DefaultListableBeanFactory类型的,
AnnotationConfigApplicationContext的父类是GenericApplicationContext,而且实现了BeanDefinitionRegistry接口
而且还使用了ConfigurableListableBeanFactory接口进行接收。
自然地,程序就会进入到if判断语句中,进来以后呢,我们来大致地分析一下下面的流程。
stepinto进入PostProcessorRegistrationDelegate._invokeBeanFactoryPostProcessors_
首先,映入眼帘的是一个for循环,它是来循环遍历invokeBeanFactoryPostProcessors方法中的第二个参数的,即beanFactoryPostProcessors。
其实呢,就是拿到所有的BeanFactoryPostProcessor,再挨个遍历出来。然后,再来以遍历出来的每一个BeanFactoryPostProcessor是否实现了BeanDefinitionRegistryPostProcessor接口为依据将其分别存放于以下两个箭头所指向的LinkedList中,其中实现了BeanDefinitionRegistryPostProcessor接口的还会被直接调用。即registryProcessor.postProcessBeanDefinitionRegistry(registry);
往下看这段源码超级长:
public static void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {// Invoke BeanDefinitionRegistryPostProcessors first, if any.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);}}// 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<>();// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.String[] postProcessorNames =beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);for (String ppName : postProcessorNames) {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, beanFactory.getApplicationStartup());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, beanFactory.getApplicationStartup());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, beanFactory.getApplicationStartup());currentRegistryProcessors.clear();}// Now, invoke the postProcessBeanFactory callback of all processors handled so far.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!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();}
组件BeanDefinitionRegistryPostProcessor
执行postProcessBeanDefinitionRegistry方法
继续按下F6快捷键让程序往下运行,直至运行到下面这行代码处,可以看到现在是会拿到所有BeanDefinitionRegistryPostProcessor的这些bean的名字。
然后执行实现了PriorityOrdered优先级接口的BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry方法
有意思的是,如果说你留心的话,那么你会发现每次执行前,都会运行完这么一行代码:beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
这行代码的意思,我上面已经说过了,就是来获取容器中所有实现了BeanDefinitionRegistryPostProcessor接口的组件。那么,为什么每次执行前,都会运行这样一行代码呢?这是因为我们每次执行可能会加载进来新的BeanDefinition,所以每次都要重新获取。
继续按下F6快捷键让程序往下运行,往下运行一步即可,Inspect一下postProcessorNames变量的值,你会发现从IOC容器中拿到的只有一个名字为org.springframework.context.annotation.internalConfigurationAnnotationProcessor的组件,即默认拿到的是ConfigurationClassPostProcessor这样一个BeanDefinitionRegistryPostProcessor。
这里我稍微提一嘴,第一次获取容器中所有实现了BeanDefinitionRegistryPostProcessor接口的组件时,其实只能获取到ConfigurationClassPostProcessor,因为我们手工加的只是BeanDefinition,等ConfigurationClassPostProcessor把对应的Definition加载后,下面才能获取到我们手工加载的BeanDefinition。
不扯远了,我们还是回到程序中来。获取到容器中所有BeanDefinitionRegistryPostProcessor组件之后,接下来,就得遍历所有这些BeanDefinitionRegistryPostProcessor组件了,挨个遍历出来之后,会判断每一个BeanDefinitionRegistryPostProcessor组件是不是实现了PriorityOrdered这个优先级接口,若是,则会先按照优先级排个序,然后再调用该组件的postProcessBeanDefinitionRegistry方法。
继续按下F6快捷键让程序往下运行,直至运行到下面这行代码处,这儿就是来执行每一个实现了PriorityOrdered优先级接口的BeanDefinitionRegistryPostProcessor组件的方法的。
我们不妨按下F5快捷键进入该方法中去看一看,如下图所示,可以看到这儿确实是来执行BeanDefinitionRegistryPostProcessor组件的postProcessBeanDefinitionRegistry方法的。
执行实现了Ordered顺序接口的BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry方法
继续按下F6快捷键让程序往下运行,直至运行到下面这行代码处,可以看到在每次执行前都会执行下面一行代码,
String[] postProcessorNames =beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
这是因为我们每次执行可能会加载进来新的BeanDefinition,所以每次都要重新获取所有实现了BeanDefinitionRegistryPostProcessor接口的组件。其实,我在上面已经讲过一遍了,这里再讲一遍,大家可一定要注意哟😀
很明显,这儿是来执行实现了Ordered顺序接口的BeanDefinitionRegistryPostProcessor组件的方法的。
原理同上面都是一模一样的,都是获取到容器中所有BeanDefinitionRegistryPostProcessor组件,紧接着再来遍历所有这些BeanDefinitionRegistryPostProcessor组件,挨个遍历出来之后,会判断每一个BeanDefinitionRegistryPostProcessor组件是不是实现了Ordered这个顺序接口,若是,则会先按照指定顺序来排个序,然后再调用该组件的postProcessBeanDefinitionRegistry方法。
执行没有实现任何优先级或者是顺序接口的BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry方法
继续按下F6快捷键让程序往下运行,直至运行到下面这行代码处。
很明显,这块是来执行没有实现任何优先级或者是顺序接口的BeanDefinitionRegistryPostProcessor组件的方法的。
原理基本同上,首先获取到容器中所有BeanDefinitionRegistryPostProcessor组件,然后遍历所有这些BeanDefinitionRegistryPostProcessor组件,挨个遍历出来之后,接着再调用该组件的postProcessBeanDefinitionRegistry方法。
执行postProcessBeanFactory方法
继续按下F6快捷键让程序往下运行,直至运行到下面这行代码处,这时,你会发现Eclipse控制台有内容输出。
很明显,这是咱们自己编写的MyBeanDefinitionRegistryPostProcessor类中的postProcessBeanDefinitionRegistry方法执行之后所输出的信息。
因为BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口,所以,接下来还得执行BeanDefinitionRegistryPostProcessor组件里面的postProcessBeanFactory方法。
按下F6快捷键让程序往下运行,往下运行一步即可,这时,你同样会发现Eclipse控制台有内容输出。
很明显,这是咱们自己编写的MyBeanDefinitionRegistryPostProcessor类中的postProcessBeanFactory方法执行之后所输出的信息。
也就是说,对于BeanDefinitionRegistryPostProcessor组件来说,它里面postProcessBeanDefinitionRegistry方法会先被调用,postProcessBeanFactory方法会后被调用。
组件BeanFactoryPostProcessor
在《Spring注解驱动开发第37讲——你知道Spring中BeanDefinitionRegistryPostProcessor是如何执行的吗?》这一讲中,我们就知道了,BeanDefinitionRegistryPostProcessor是要优先于BeanFactoryPostProcessor执行的。在上面已经执行完了BeanDefinitionRegistryPostProcessor的方法,接下来就得来执行BeanFactoryPostProcessor的方法了。
执行的流程是怎样的呢?我们可以大致地来看一下,按下F6快捷键让程序往下运行,直至程序运行到以下这行代码处,可以看到现在是来从beanFactory中按照类型获取所有BeanFactoryPostProcessor组件的名字。
获取到所有BeanFactoryPostProcessor组件之后,接下来,就得遍历所有这些BeanFactoryPostProcessor组件了,挨个遍历出来之后,按照是否实现了PriorityOrdered接口、Ordered接口以及没有实现这两个接口这三种情况进行分类,将其分别存储于三个ArrayList中。
执行postProcessBeanFactory方法
紧接着,按照顺序依次执行BeanFactoryPostProcessors组件对应的postProcessBeanFactory方法。
也就是说,先来执行实现了PriorityOrdered优先级接口的BeanFactoryPostProcessor组件的postProcessBeanFactory方法,再来执行实现了Ordered顺序接口的BeanFactoryPostProcessor组件的postProcessBeanFactory方法,最后再来执行没有实现任何优先级或者是顺序接口的BeanFactoryPostProcessor组件的postProcessBeanFactory方法。
你有没有发现,程序直至到这儿,才是来执行所有BeanFactoryPostProcessor组件的postProcessBeanFactory方法的呢?
继续按下F6快捷键让程序往下运行,直至程序运行到以下这行代码处,这时,你会发现Eclipse控制台有内容输出。
很明显,这是咱们自己编写的MyBeanFactoryPostProcessor类中的postProcessBeanFactory方法执行之后所输出的信息。
demo如下:MyBeanFactoryPostProcessor
package com.meimeixia.ext;import java.util.Arrays;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanFactoryPostProcessor;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.stereotype.Component;@Componentpublic class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {System.out.println("MyBeanFactoryPostProcessor...postProcessBeanFactory..."); // 这个时候我们所有的bean还没被创建// 但是我们可以看一下通过Spring给我们传过来的这个beanFactory,我们能拿到什么int count = beanFactory.getBeanDefinitionCount(); // 我们能拿到有几个bean定义String[] names = beanFactory.getBeanDefinitionNames(); // 除此之外,我们还能拿到每一个bean定义的名字System.out.println("当前BeanFactory中有" + count + "个Bean");System.out.println(Arrays.asList(names));}}
MyBeanDefinitionRegistryPostProcessor
package com.meimeixia.ext;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.beans.factory.support.AbstractBeanDefinition;import org.springframework.beans.factory.support.BeanDefinitionBuilder;import org.springframework.beans.factory.support.BeanDefinitionRegistry;import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;import org.springframework.beans.factory.support.RootBeanDefinition;import org.springframework.stereotype.Component;import com.meimeixia.bean.Blue;// 记住,我们这个组件写完之后,一定别忘了给它加在容器中@Componentpublic class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {// TODO Auto-generated method stubSystem.out.println("MyBeanDefinitionRegistryPostProcessor...bean的数量:" + beanFactory.getBeanDefinitionCount());}/*** 这个BeanDefinitionRegistry就是Bean定义信息的保存中心,这个注册中心里面存储了所有的bean定义信息,* 以后,BeanFactory就是按照BeanDefinitionRegistry里面保存的每一个bean定义信息来创建bean实例的。** bean定义信息包括有哪些呢?有这些,这个bean是单例的还是多例的、bean的类型是什么以及bean的id是什么。* 也就是说,这些信息都是存在BeanDefinitionRegistry里面的。*/@Overridepublic void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {// TODO Auto-generated method stubSystem.out.println("postProcessBeanDefinitionRegistry...bean的数量:" + registry.getBeanDefinitionCount());// 除了查看bean的数量之外,我们还可以给容器里面注册一些bean,我们以前也简单地用过/** 第一个参数:我们将要给容器中注册的bean的名字* 第二个参数:BeanDefinition对象*/// RootBeanDefinition beanDefinition = new RootBeanDefinition(Blue.class); // 现在我准备给容器中添加一个Blue对象// 咱们也可以用另外一种办法,即使用BeanDefinitionBuilder这个构建器生成一个BeanDefinition对象,很显然,这两种方法的效果都是一样的AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Blue.class).getBeanDefinition();registry.registerBeanDefinition("hello", beanDefinition);}}
继续按下F6快捷键让程序往下运行,直至程序运行到以下这行代码处,这时,invokeBeanFactoryPostProcessors方法才总算是执行完了。
至此,我们知道了一点,那就是invokeBeanFactoryPostProcessors方法最主要的核心作用就是执行了BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry和postProcessBeanFactory这俩方法,以及BeanFactoryPostProcessors的postProcessBeanFactory方法。
也就是说组件BeanDefinitionRegistryPostProcessor是要优先于组件BeanFactoryPostProcessor执行的。
