invokeBeanFactoryPostProcessors详

2022-05-11  本文已影响0人  养一只tom猫

invokeBeanFactoryPostProcessors该方法会实例化所有BeanDefinitionRegistryPostProcessor和BeanFactoryPostProcessor的实例并且执行postProcessBeanFactory与postProcessBeanDefinitionRegistry方法。

BeanDefinitionRegistryPostProcessor:该接口继承了BeanFactoryPostProcessor。


image.png

所以实现该接口需要重写两个方法postProcessBeanFactory与postProcessBeanDefinitionRegistry。

postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)该方法参数为BeanDefinitionRegistry该接口提供了对BeanDefinition的操作。
postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)该方法提供了对BeanFactory的操作。

在读invokeBeanFactoryPostProcessors方法之前,先梳理一下BeanFactoryPostProcessor执行流程:外部BeanDefinitionRegistryPostProcessor-->实现了PriorityOrdered的BeanDefinitionRegistryPostProcessor-->实现了Ordered的BeanDefinitionRegistryPostProcessor-->无排序的BeanDefinitionRegistryPostProcessor-->调用BeanDefinitionRegistryPostProcessor#postProcessBeanFactory-->外部BeanFactoryPostProcessor-->实现了PriorityOrdered的BeanFactoryPostProcessor-->实现了Ordered的BeanFactoryPostProcessor-->无排序的BeanFactoryPostProcessor

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

        // Invoke BeanDefinitionRegistryPostProcessors first, if any.
        // 将已经执行过的BeanFactoryPostProcessor存储在processedBeans,防止重复执行
        Set<String> processedBeans = new HashSet<>();

        // 此处条件成立,BeanFactory类型为DefaultListableBeanFactory,而DefaultListableBeanFactory实现了BeanDefinitionRegistry接口
        if (beanFactory instanceof BeanDefinitionRegistry) {
            BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
            // 用来存放BeanFactoryPostProcessor对象
            List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
            // 用来存放BeanDefinitionRegistryPostProcessor对象
            // 方便统一执行实现了BeanDefinitionRegistryPostProcessor接口父类的方法
            List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

            // 处理外部定义的BeanFactoryPostProcessor,将BeanDefinitionRegistryPostProcessor与BeanFactoryPostProcessor区分开
            for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
                // 如果为BeanDefinitionRegistryPostProcessor类型
                if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                    // 强转成BeanDefinitionRegistryPostProcessor
                    BeanDefinitionRegistryPostProcessor registryProcessor =
                            (BeanDefinitionRegistryPostProcessor) postProcessor;
                    // 如果是BeanDefinitionRegistryPostProcessor类型,直接执行postProcessBeanDefinitionRegistry方法
                    registryProcessor.postProcessBeanDefinitionRegistry(registry);
                    registryProcessors.add(registryProcessor);
                }
                else {
                    // 如果不是BeanDefinitionRegistryPostProcessor类型
                    // 则将外部集合中的BeanFactoryPostProcessor存放到regularPostProcessors用于后续一起执行
                    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存放当前需要执行的BeanDefinitionRegistryPostProcessor
            List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

            // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
            // TODO 首先,调用实现 PriorityOrdered 的 BeanDefinitionRegistryPostProcessor。
            // 获取所有实现了BeanDefinitionRegistryPostProcessor接口的类名
            String[] postProcessorNames =
                    beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            for (String ppName : postProcessorNames) {
                // 判断当前类是否实现了PriorityOrdered接口
                if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                    // 将BeanDefinitionRegistryPostProcessor存入currentRegistryProcessors
                    currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    // 提前存放到processedBeans,避免重复执行,但是此处还未执行
                    processedBeans.add(ppName);
                }
            }
            // 对currentRegistryProcessors接口中的BeanDefinitionRegistryPostProcessor进行排序,方便后续执行
            sortPostProcessors(currentRegistryProcessors, beanFactory);
            // 添加到registryProcessors集合,用于后续执行父接口的postProcessBeanFactory方法
            registryProcessors.addAll(currentRegistryProcessors);
            // 遍历集合,执行BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry()方法
            invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
            // 执行完毕后,将currentRegistryProcessors清空
            currentRegistryProcessors.clear();

            // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
            // TODO 接下来,调用实现 Ordered 的 BeanDefinitionRegistryPostProcessors。
            // 这里为什么要再次获取BeanDefinitionRegistryPostProcessor
            // 是因为有可能在上面方法执行过程中添加了BeanDefinitionRegistryPostProcessor,所以这里再次获取
            // 而下面处理BeanFactoryPostProcessor的时候又不需要重复获取了是为什么呢?
            // 因为添加BeanFactoryPostProcessor与BeanDefinitionRegistryPostProcessor只能在BeanDefinitionRegistryPostProcessor
            // 中添加,在BeanFactoryPostProcessor是无法添加的,具体看方法参数就懂了
            postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            for (String ppName : postProcessorNames) {
                // 判断当前bean没有被执行过,并且实现了Ordered接口
                if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
                    // getBean() 如果BeanFactory中没有该Bean则会去创建该Bean
                    currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    processedBeans.add(ppName);
                }
            }
            // 以下操作和上面是一样的,排序-->添加到registryProcessors-->执行-->清空currentRegistryProcessors
            sortPostProcessors(currentRegistryProcessors, beanFactory);
            registryProcessors.addAll(currentRegistryProcessors);
            invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
            currentRegistryProcessors.clear();

            // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
            // TODO 最后处理没有实现Ordered与PriorityOrdered接口的BeanDefinitionRegistryPostProcessor
            boolean reiterate = true;
            while (reiterate) {
                reiterate = false;
                // 再次获取BeanDefinitionRegistryPostProcessor
                postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
                for (String ppName : postProcessorNames) {
                    if (!processedBeans.contains(ppName)) {
                        // 将本次要执行的BeanDefinitionRegistryPostProcessor存放到currentRegistryProcessors
                        currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                        // 提前存放到processedBeans,避免重复执行
                        processedBeans.add(ppName);
                        reiterate = true;
                    }
                }
                // 此处的排序已经没有意义了
                sortPostProcessors(currentRegistryProcessors, beanFactory);
                // 将本次执行的BeanDefinitionRegistryPostProcessor添加到registryProcessors
                registryProcessors.addAll(currentRegistryProcessors);
                // 执行
                invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
                // 清空
                currentRegistryProcessors.clear();
            }

            // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
            // TODO 现在,调用到目前为止处理的所有处理器的 postProcessBeanFactory 回调。
            invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
            invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
        }

        else {
            // Invoke factory processors registered with the context instance.
            // BeanFactory如果不归属于BeanDefinitionRegistry类型,则直接执行beanFactoryPostProcessor
            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.
        // 用于存放实现了priorityOrdered接口的BeanFactoryPostProcessor
        List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
        // 用于存放实现了ordered接口的BeanFactoryPostProcessor名称
        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
            }
            // 如果实现了PriorityOrdered
            else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
            }
            // 如果实现了Ordered
            else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
                orderedPostProcessorNames.add(ppName);
            }
            // 无排序
            else {
                nonOrderedPostProcessorNames.add(ppName);
            }
        }

        // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
        // TODO 首先,调用实现 PriorityOrdered 的 BeanFactoryPostProcessor。
        sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
        invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

        // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
        // TODO 接下来,调用实现 Ordered 的 BeanFactoryPostProcessors。
        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.
        // TODO 最后,调用所有其他 BeanFactoryPostProcessor。
        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();
    }

在上面方法中可以发现Spring重复多次获取了BeanDefinitionRegistryPostProcessor的BeanNames而后续处理BeanFactoryPostProcessor时候并没有重复获取,为什么呢?
原因是BeanDefinitionRegistryPostProcessor提供的方法是操作BeanDefinition的,而在这个方法中是可以再次注册BeanDefinitionRegistryPostProcessor/BeanFactoryPostProcessor的。而BeanFactoryPostProcessor并没有提供方式注册。

上一篇下一篇

猜你喜欢

热点阅读