关于spring:Spring源码分析invokeBeanFactoryPostProcessors介绍

5次阅读

共计 10228 个字符,预计需要花费 26 分钟才能阅读完成。

1.BeanFactoryPostProcessors 和 BeanDefinitionRegistryPostProcessor 的作用

2.spring 源码 prepareBeanFactory(beanFactory)流程介绍

3.spring 源码 prepareBeanFactory(beanFactory)源码解析

4. 总结

1.BeanFactoryPostProcessors 和 BeanDefinitionRegistryPostProcessor 的作用

github 源码地址(带正文):
https://github.com/su15967456…

spring 源码执行流程图:

咱们点到这个办法里,大抵浏览一下代码,发现次要是围绕着这两个汇合进行操作

简而言之:
invokeBeanFactoryPostProcessors办法会把所有实现 beanFactoryPostProcessor 和 BeanDefinitionRegistryPostProcessor 的类进行 实例化和调用

咱们先来看一下 beanFactoryPostProcessor 的正文:

意思大抵为:在 bean definitions 全副加载结束,并且在初始化之前,beanFactoryPostProcessor 能够对这些 bd 重写或者减少一些属性

咱们再来看一下 BeanDefinitionRegistryPostProcessor 的正文:

当 bean definitions 全副被加载结束,并且在初始化之前,BeanDefinitionRegistryPostProcessor减少一些额定的 bean definition。

咱们得出结论:
BeanDefinitionRegistryPostProcessor:能够用来减少新的 bean Difinition
beanFactoryPostProcessor:能够对 bean Difinition 的进行批改。

2.spring 源码 prepareBeanFactory(beanFactory)流程介绍

从整体的执行程序来看,这个办法的执行流程是这个样子的:

1)执行内部传进来的 BeanFactoryPostProcessor 类
2) 执行实现子类 BeanDefinitionRegistryPostProcessor 接口的类
3) 执行实现父类 BeanFactoryPostProcessor 接口的类

从执行每一步的 BeanDefinitionRegistryPostProcessor 或者 BeanFactoryPostProcessor,又能够分为以下几个逻辑:


1)执行实现了 PriorityOrdered(高优先级)的类
2) 执行实现了 Ordered(有序)的类
3) 执行什么都没有实现的一般类

3.spring 源码 prepareBeanFactory(beanFactory)源码解析

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

        // WARNING: Although it may appear that the body of this method can be easily
        // refactored to avoid the use of multiple loops and multiple lists, the use
        // of multiple lists and multiple passes over the names of processors is
        // intentional. We must ensure that we honor the contracts for PriorityOrdered
        // and Ordered processors. Specifically, we must NOT cause processors to be
        // instantiated (via getBean() invocations) or registered in the ApplicationContext
        // in the wrong order.
        //
        // Before submitting a pull request (PR) to change this method, please review the
        // list of all declined PRs involving changes to PostProcessorRegistrationDelegate
        // to ensure that your proposal does not result in a breaking change:
        // https://github.com/spring-projects/spring-framework/issues?q=PostProcessorRegistrationDelegate+is%3Aclosed+label%3A%22status%3A+declined%22

        // Invoke BeanDefinitionRegistryPostProcessors first, if any.
        // 无论是什么状况,先执行 BeanDefinitionRegistryPostProcessors
        // 将曾经执行的 BFPP 存储在 processBean 中,避免反复执行
        Set<String> processedBeans = new HashSet<>();
        //BeanDefinitionRegistry 是对 beanDefinition 进行操作的类
        // 判断 beanFactory 是不是 BeanDefinitionRegistry 的实现,此处是 DefaultListableBeanFactory, 实现了 BeanDefinitionRegistry 接口,此处为 true
        if (beanFactory instanceof BeanDefinitionRegistry) {
            // 类型转换
            BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
            // 此时要做一个辨别,BeanDefinitionRegistryPostProcessor 是 BeanFactoryPostProcessor 的子类
            //BeanFactoryPostProcessor 次要针对的对象是 BeanFactory,//BeanDefinitionRegistryPostProcessor 次要针对的对象是 BeanDefinition
            // 寄存 BeanFactoryPostProcessor 的汇合类
            List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
            // 寄存 BeanDefinitionRegistryPostProcessor 的汇合
            List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

            // 首先解决入参中的 beanFactoryPostProcessors,遍历所有的 beanFactoryPostProcessors
            for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
                // 如果是 BeanDefinitionRegistryPostProcessor
                if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                    BeanDefinitionRegistryPostProcessor registryProcessor =
                            (BeanDefinitionRegistryPostProcessor) postProcessor;
                    // 查找 BeanDefinitionRegistryPostProcessor 中的 postProcessBeanDefinitionRegistry 办法
                    registryProcessor.postProcessBeanDefinitionRegistry(registry);
                    // 增加到 registryProcessors
                    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.
            // 用于保留本次要执行的 BeanDefinitionRegistryPostProcessor
            List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

            // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
            // 调用所有实现 PriorityOrdered 接口的 BeanDefinitionRegistryPostProcessor 实现类
            // 找到所有实现 BeanDefinitionRegistryPostProcessor 接口 bean 的 beanName
            String[] postProcessorNames =
                    beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            // 遍历所有合乎规定的 postProcessNames
            for (String ppName : postProcessorNames) {
                // 检测是否实现了 PriorityOrdered 接口
                if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                    // 获取名字对应的 bean 实例,增加到 currentRegistryProcessor
                    currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    // 将要被执行的 BFPP 增加到 processedBeans 中,避免反复执行
                    processedBeans.add(ppName);
                }
            }
            // 依照优先程序进行排序
            sortPostProcessors(currentRegistryProcessors, beanFactory);
            // 增加到 registryProcessors 中,用于最初执行 postProcessBeanFactory 办法
            registryProcessors.addAll(currentRegistryProcessors);
            // 遍历 currentRegistryProcessors,执行 postProcessBeanFactory 办法
            invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
            // 执行结束后,清空
            currentRegistryProcessors.clear();

            // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
            // 调用所有实现 Ordered 接口的 BeanDefinitionRegistryPostProcessor 实现类
            // 找到所有实现 BeanDefinitionRegistryPostProcessor 接口 bean 的 beanName
            // 为什么要从新获取:// 下面调用 invoke 办法的时候,可能会新增一些 BeanDefinitionRegistryPostProcessor
            postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            for (String ppName : postProcessorNames) {
                // 检测是否实现了 Order 接口,并且还未执行过程
                if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
                    // 获取名字的 bean 实例,增加到 currentRegistryProcessors
                    currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    // 增加到已执行过的 processedBeans
                    processedBeans.add(ppName);
                }
            }
            // 依照优先程序进行排序
            sortPostProcessors(currentRegistryProcessors, beanFactory);
            // 增加到 registryProcessors 中,用于最初执行 postProcessBeanFactory 办法
            registryProcessors.addAll(currentRegistryProcessors);
            // 遍历 currentRegistryProcessors,执行 postProcessBeanFactory 办法
            invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
            // 执行结束后,清空
            currentRegistryProcessors.clear();

            // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
            // 最初,调用剩下的 BeanDefinitionRegistryPostProcessors,没有实现 Order 的
            boolean reiterate = true;
            while (reiterate) {
                reiterate = false;
                // 找出所有 BeanDefinitionRegistryPostProcessors 的接口类
                postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
                for (String ppName : postProcessorNames) {
                    // 如果还未执行过 BeanDefinitionRegistryPostProcessors
                    if (!processedBeans.contains(ppName)) {currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                        processedBeans.add(ppName);
                        // 如果在中途过程中,可能会新增 BeanDefinitionRegistryPostProcessors,所以这里要为 true
                        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.
            // 执行 postProcessBeanFactory
            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 的所有类
        // 到目前为止,所有 BeanDefinitionRegistryPostProcessor 曾经全副实现结束了,接下来开始 BeanFactoryPostProcessor 的类的解决
        String[] postProcessorNames =
                beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

        // 他们不反复执行是因为 beanFactoryPostProcessor 不会新增新 Processor
        // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
        // Ordered, and the rest.
        // 下面只执行了实现 BeanDefinitionRegistryPostProcessor 的 postprocessor,并没有实现
        // 有 priorityOrdered 的 PostProcessors 汇合
        List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
        // 有 ordered 的 PostProcessors 汇合 为什么上面两种存 string,下面那种存类
        // 代码改掉还是能够运行的,猜想可能是省空间
        List<String> orderedPostProcessorNames = new ArrayList<>();
        // 没有 order 的 PostProcessors 汇合
        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.
        // 依据 priorityOrderedPostProcessors 的汇合先排序,后执行
        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();}

以上的几个逻辑,除了执行程序外,有几个重要的点:
1)为什么执行子类 BeanDefinitionRegistryPostProcessor 的时候,每次都要从容器中从新获取类
因为实现 BeanDefinitionRegistryPostProcessor 的类 能够减少新的 bd,也就是说能够减少新的 BeanDefinitionRegistryPostProcessor,所以每次都要从新获取
2)为什么执行父类 BeanFactoryPostProcessor 的时候, 不必从新获取
因为父类 BeanFactoryPostProcessor,不能减少新的 bd,所以就不必从新获取了。
3)

4. 总结
咱们能够看出,这个办法的执行逻辑还是比较简单和容易了解,而且都是由一个函数编写而成,封装地不是太厉害。

正文完
 0