Spring源码之invokeBeanFactoryPostProcessors

概述

invokeBeanFactoryPostProcessors办法中次要是解决BeanDefinitionRegistryPostProcessor和BeanFactoryPostProcessor,BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口,具备更弱小的性能。这两个接口是Spring再初始化前一个重要的扩大点。

invokeBeanFactoryPostProcessors源码剖析

public static void invokeBeanFactoryPostProcessors(        ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {     // Invoke BeanDefinitionRegistryPostProcessors first, if any.    Set<String> processedBeans = new HashSet<String>();     // 判断beanFactory是否为BeanDefinitionRegistry,beanFactory为DefaultListableBeanFactory,    // 而DefaultListableBeanFactory实现了BeanDefinitionRegistry接口,因而这边为true    if (beanFactory instanceof BeanDefinitionRegistry) {        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;        // 用于寄存一般的BeanFactoryPostProcessor        List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<BeanFactoryPostProcessor>();        // 用于寄存BeanDefinitionRegistryPostProcessor        List<BeanDefinitionRegistryPostProcessor> registryProcessors = new LinkedList<BeanDefinitionRegistryPostProcessor>();         // 首先解决入参中的beanFactoryPostProcessors        // 遍历所有的beanFactoryPostProcessors, 将BeanDefinitionRegistryPostProcessor和一般BeanFactoryPostProcessor辨别开        for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {            if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {                //  如果是BeanDefinitionRegistryPostProcessor                BeanDefinitionRegistryPostProcessor registryProcessor =                        (BeanDefinitionRegistryPostProcessor) postProcessor;                // 间接执行BeanDefinitionRegistryPostProcessor接口的postProcessBeanDefinitionRegistry办法                registryProcessor.postProcessBeanDefinitionRegistry(registry); //@3                //  增加到registryProcessors(用于最初执行postProcessBeanFactory办法)                registryProcessors.add(registryProcessor);            } else {                // 否则,只是一般的BeanFactoryPostProcessor                // 增加到regularPostProcessors(用于最初执行postProcessBeanFactory办法)                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<BeanDefinitionRegistryPostProcessor>();         // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.        // 调用所有实现PriorityOrdered接口的BeanDefinitionRegistryPostProcessor实现类        //  找出所有实现BeanDefinitionRegistryPostProcessor接口的Bean的beanName        //@1        String[] postProcessorNames =                beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);        //遍历postProcessorNames        for (String ppName : postProcessorNames) {            // 校验是否实现了PriorityOrdered接口            if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {                //  获取ppName对应的bean实例, 增加到currentRegistryProcessors中,                // beanFactory.getBean: 这边getBean办法会触发创立ppName对应的bean对象, 目前暂不深刻解析                currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));                // 将要被执行的退出processedBeans,防止后续反复执行                processedBeans.add(ppName);            }        }        //进行排序(依据是否实现PriorityOrdered、Ordered接口和order值来排序)        sortPostProcessors(currentRegistryProcessors, beanFactory);        //  增加到registryProcessors(用于最初执行postProcessBeanFactory办法)        registryProcessors.addAll(currentRegistryProcessors);        // 遍历currentRegistryProcessors, 执行postProcessBeanDefinitionRegistry办法        invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);        // 执行结束后, 清空currentRegistryProcessors        currentRegistryProcessors.clear();         // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.        // 调用所有实现了Ordered接口的BeanDefinitionRegistryPostProcessor实现类(过程跟下面的步骤3根本一样)        // 找出所有实现BeanDefinitionRegistryPostProcessor接口的类, 这边反复查找是因为执行完下面的BeanDefinitionRegistryPostProcessor,        // 可能会新增了其余的BeanDefinitionRegistryPostProcessor, 因而须要从新查找       // @2        postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);        for (String ppName : postProcessorNames) {            // 校验是否实现了Ordered接口,并且还未执行过            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);        //  遍历currentRegistryProcessors, 执行postProcessBeanDefinitionRegistry办法        invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);        currentRegistryProcessors.clear();         // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.        // 最初, 调用所有剩下的BeanDefinitionRegistryPostProcessors        boolean reiterate = true;        while (reiterate) {            reiterate = false;            // 找出所有实现BeanDefinitionRegistryPostProcessor接口的类            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);                    //  如果有BeanDefinitionRegistryPostProcessor被执行, 则有可能会产生新的BeanDefinitionRegistryPostProcessor,                    // 因而这边将reiterate赋值为true, 代表须要再循环查找一次                    reiterate = true;                }            }            sortPostProcessors(currentRegistryProcessors, beanFactory);            registryProcessors.addAll(currentRegistryProcessors);            // 遍历currentRegistryProcessors, 执行postProcessBeanDefinitionRegistry办法            invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);            currentRegistryProcessors.clear();        }         // Now, invoke the postProcessBeanFactory callback of all processors handled so far.        //调用所有BeanDefinitionRegistryPostProcessor的postProcessBeanFactory办法(BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProcessor)        invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);        // 最初, 调用入参beanFactoryPostProcessors中的一般BeanFactoryPostProcessor的postProcessBeanFactory办法        invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);    } else {        // Invoke factory processors registered with the context instance.        invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);    }     // 到这里 , 入参beanFactoryPostProcessors和容器中的所有BeanDefinitionRegistryPostProcessor曾经全副处理完毕,    // 上面开始解决容器中的所有BeanFactoryPostProcessor     // Do not initialize FactoryBeans here: We need to leave all regular beans    // uninitialized to let the bean factory post-processors apply to them!    // 找出所有实现BeanFactoryPostProcessor接口的类    //@3    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<BeanFactoryPostProcessor>();    // 用于寄存实现了Ordered接口的BeanFactoryPostProcessor的beanName    List<String> orderedPostProcessorNames = new ArrayList<String>();    // 用于寄存一般BeanFactoryPostProcessor的beanName    List<String> nonOrderedPostProcessorNames = new ArrayList<String>();    // 遍历postProcessorNames, 将BeanFactoryPostProcessor按实现PriorityOrdered、实现Ordered接口、一般三种辨别开    for (String ppName : postProcessorNames) {        //  跳过曾经执行过的        if (processedBeans.contains(ppName)) {            // skip - already processed in first phase above        } else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {            // 增加实现了PriorityOrdered接口的BeanFactoryPostProcessor            priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));        } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {            //  增加实现了Ordered接口的BeanFactoryPostProcessor的beanName            orderedPostProcessorNames.add(ppName);        } else {            // 增加剩下的一般BeanFactoryPostProcessor的beanName            nonOrderedPostProcessorNames.add(ppName);        }    }     // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.    // 调用所有实现PriorityOrdered接口的BeanFactoryPostProcessor    // 对priorityOrderedPostProcessors排序    sortPostProcessors(priorityOrderedPostProcessors, beanFactory);    // 遍历priorityOrderedPostProcessors, 执行postProcessBeanFactory办法    invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);     // Next, invoke the BeanFactoryPostProcessors that implement Ordered.    // 调用所有实现Ordered接口的BeanFactoryPostProcessor    List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();    for (String postProcessorName : orderedPostProcessorNames) {        //  获取postProcessorName对应的bean实例, 增加到orderedPostProcessors, 筹备执行        orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));    }    //  对orderedPostProcessors排序    sortPostProcessors(orderedPostProcessors, beanFactory);    // 遍历orderedPostProcessors, 执行postProcessBeanFactory办法    invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);     // Finally, invoke all other BeanFactoryPostProcessors.    // 调用所有剩下的BeanFactoryPostProcessor    List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();    for (String postProcessorName : nonOrderedPostProcessorNames) {        // 11.1 获取postProcessorName对应的bean实例, 增加到nonOrderedPostProcessors, 筹备执行        nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));    }    // 遍历nonOrderedPostProcessors, 执行postProcessBeanFactory办法    invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);     // Clear cached merged bean definitions since the post-processors might have    // modified the original metadata, e.g. replacing placeholders in values...    // 革除元数据缓存(mergedBeanDefinitions、allBeanNamesByType、singletonBeanNamesByType),    // 因为后处理器可能曾经批改了原始元数据,例如, 替换值中的占位符...    beanFactory.clearMetadataCache();}

@1这里会咱们能够看下beanFactory中只有7个beanDefinition,其中只有ConfigurationClassPostProcessor是实现了BeanDefinitionRegistryPostProcessor接口
所以它会被优先执行。

ConfigurationClassPostProcessor后置处理器是在run()--->createApplicationContext()--->ApplicationContextFactory.create()--->
AnnotationConfigApplicationContext---->AnnotatedBeanDefinitionReader()--->
AnnotationConfigUtils.registerAnnotationConfigProcessors()增加的默认处理器。
@2因为加载了ConfigurationClassPostProcessor,零碎中可能新增一些实现了BeanDefinitionRegistryPostProcessor的类,对他们进行排序,遍历执行postProcessBeanDefinitionRegistry办法。
@3加载所有实现了BeanFactoryPostProcessor,执行postProcessBeanFactory办法
从代码执行的程序也能够晓得BeanDefinitionRegistryPostProcessor是在BeanFactoryPostProcessor之前执行

测试论证

@Componentpublic class BeanDefinitionRegistryPostProcessorOrder1 implements BeanDefinitionRegistryPostProcessor, Ordered {    @Override    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {        System.out.println("BeanDefinitionRegistryPostProcessorOrder1:::"+"postProcessBeanDefinitionRegistry");    }    @Override    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {        System.out.println("BeanDefinitionRegistryPostProcessorOrder1:::"+"postProcessBeanFactory");    }    @Override    public int getOrder() {        return 1;    }}@Componentpublic class BeanDefinitionRegistryPostProcessorOrder2 implements BeanDefinitionRegistryPostProcessor, Ordered {    @Override    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {        System.out.println("BeanDefinitionRegistryPostProcessorOrder2:::" + "postProcessBeanFactory");    }    @Override    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {        System.out.println("BeanDefinitionRegistryPostProcessorOrder2:::" + "postProcessBeanDefinitionRegistry");    }        @Override    public int getOrder() {        return 2;    }}@Componentpublic class BeanFactoryPostProcessorOrder1 implements BeanFactoryPostProcessor, Ordered {    @Override    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {        System.out.println("BeanFactoryPostProcessorOrder1:::"+"postProcessBeanFactory");    }    @Override    public int getOrder() {        return 1;    }}@Componentpublic class BeanFactoryPostProcessorOrder2 implements BeanFactoryPostProcessor, Ordered {    @Override    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {        System.out.println("BeanFactoryPostProcessorOrder2:::" + "postProcessBeanFactory");    }    @Override    public int getOrder() {        return 2;    }}输入后果:BeanDefinitionRegistryPostProcessorOrder1:::postProcessBeanDefinitionRegistryBeanDefinitionRegistryPostProcessorOrder2:::postProcessBeanDefinitionRegistryBeanDefinitionRegistryPostProcessorOrder1:::postProcessBeanFactoryBeanDefinitionRegistryPostProcessorOrder2:::postProcessBeanFactoryBeanFactoryPostProcessorOrder1:::postProcessBeanFactoryBeanFactoryPostProcessorOrder2:::postProcessBeanFactory