spring 工具類PostProcessorRegistrationDelegate詳解


一、概述

  PostProcessorRegistrationDelegate作用就是對BeanDefinitionRegistryPostProcessor 、BeanFactoryPostProcessor、BeanPostProcessor等接口實現類執行相應方法。

  BeanDefinitionRegistryPostProcessor:可以對bean定義進行操作,注冊到容器中

  BeanFactoryPostProcessor:對bean定義進行操作,實例化之前

  BeanPostProcessor:對對象初始化前后進行操作

二、源碼部分詳解

    該類主要有兩大方法分別是

    invokeBeanFactoryPostProcessors()、registerBeanPostProcessors()

    執行時機(spring啟動刷新上下文時候依次執行這兩個方法):

    

 

  1、invokeBeanFactoryPostProcessors

   

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

    // 存儲BeanDefinitionRegistryPostProcessor接口實現類名稱,作用就是遍歷BeanFactoryPostProcessor方法時,
    //剔除掉BeanDefinitionRegistryPostProcessor實現類名,因為改實現類已經調用過BeanFactoryPostProcessor方法,下面會提到
    Set<String> processedBeans = new HashSet<>();
    //beanFactory為DefaultListableBeanFactory 實現了BeanDefinitionRegistry因此為true
    if (beanFactory instanceof BeanDefinitionRegistry) {
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
        List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
        List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
        //獲取spring自身實現BeanFactoryPostProcessor、BeanDefinitionRegistryPostProcessor對象,列如一些對象需要對注解解析掃描,
        for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
            if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                BeanDefinitionRegistryPostProcessor registryProcessor =
                        (BeanDefinitionRegistryPostProcessor) postProcessor;
                registryProcessor.postProcessBeanDefinitionRegistry(registry);
                registryProcessors.add(registryProcessor);
            }
            else {
                regularPostProcessors.add(postProcessor);
            }
        }


        //存儲spring掃描過程中當前BeanDefinitionRegistryPostProcessor實現類
        List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
        //getBeanNamesForType 獲取BeanDefinitionRegistryPostProcessor實現類bean的名稱,此時通過
        //org.springframework.context.annotation.internalConfigurationAnnotationProcessor獲取ConfigurationClassPostProcessor(掃描注解)
        String[] postProcessorNames =
                beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
        //遍歷BeanDefinitionRegistryPostProcessor實現類bean的名稱,
        for (String ppName : postProcessorNames) {
            //判斷是否實現PriorityOrdered優先級,實現了把當前bean的名稱存到processedBeans,bean對象存到currentRegistryProcessors
            if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                processedBeans.add(ppName);
            }
        }
        //進行排序
        sortPostProcessors(currentRegistryProcessors, beanFactory);
        //把currentRegistryProcessors中對象和registryProcessors合並到一起
        registryProcessors.addAll(currentRegistryProcessors);
        //遍歷registryProcessors集合中BeanDefinitionRegistryPostProcessor實現類的postProcessBeanDefinitionRegistry方法
        invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
        //清空集合中的對象
        currentRegistryProcessors.clear();

        // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
        //再次獲取BeanDefinitionRegistryPostProcessor實現類對象,為什么再次獲取,因為一些自定義BeanDefinitionRegistryPostProcessor實現類第一次沒有掃描到
        //自定義需要@component等注解,注解掃描類需要ConfigurationClassPostProcessor這個類完成,所以第一次獲取時,是獲取不到自定義實現類的
        postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
        for (String ppName : postProcessorNames) {
            //把實現Ordered對象bean的名稱放到processedBeans,當前BeanDefinitionRegistryPostProcessor實現類放到currentRegistryProcessors
            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.
        boolean reiterate = true;
        //再次獲取其他BeanDefinitionRegistryPostProcessor實現類,死循環就是保證所有的都拿到,
        //因為的實現類有可能注冊了其他BeanDefinitionRegistryPostProcessor實現類沒有掃描到
        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);
            //調用postProcessBeanDefinitionRegistry方法
            invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
            currentRegistryProcessors.clear();
        }
        
        //調用registryProcessors中后置處理器的后置方法
        invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
        //調用regularPostProcessors中后置處理器的后置方法
        invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
    }

    else {
        //一般不會走這里,擴展spring時候,工廠對象沒有實現BeanDefinitionRegistry
        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實現類bean的名稱(后置處理器)
    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) {
        //processedBeans中在上面已經執行過后置處理器中的方法
        if (processedBeans.contains(ppName)) {
            // skip - already processed in first phase above
        }
        //實現PriorityOrdered(優先級)放到priorityOrderedPostProcessors
        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);
        }
    }
    /*
     * 下面依次執行集合中后置對象的后置方法
     */
    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();
}

 

  2、registerBeanPostProcessors(注冊BeanPostProcessor實現類到容器)

  

public static void registerBeanPostProcessors(
        ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
    
    //獲取BeanPostProcessor實現類bean的名稱
    String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

    int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
    beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

    // Separate between BeanPostProcessors that implement PriorityOrdered,
    // Ordered, and the rest.
    //用於存P實現riorityOrdered
    List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
    //用於存內部的BeanPostProcessor實現類
    List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
    //用於存orderedPostProcessorNames實現類
    List<String> orderedPostProcessorNames = new ArrayList<>();
    //存其他的
    List<String> nonOrderedPostProcessorNames = new ArrayList<>();
    for (String ppName : postProcessorNames) {
        if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
            BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
            priorityOrderedPostProcessors.add(pp);
            if (pp instanceof MergedBeanDefinitionPostProcessor) {
                internalPostProcessors.add(pp);
            }
        }
        else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
            orderedPostProcessorNames.add(ppName);
        }
        else {
            nonOrderedPostProcessorNames.add(ppName);
        }
    }
    //排序
    sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
    //注冊BeanPostProcessor實現PriorityOrdered類
    registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

    // Next, register the BeanPostProcessors that implement Ordered.
    List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
    for (String ppName : orderedPostProcessorNames) {
        BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
        orderedPostProcessors.add(pp);
        if (pp instanceof MergedBeanDefinitionPostProcessor) {
            internalPostProcessors.add(pp);
        }
    }
    sortPostProcessors(orderedPostProcessors, beanFactory);
    registerBeanPostProcessors(beanFactory, orderedPostProcessors);

    // Now, register all regular BeanPostProcessors.
    List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
    for (String ppName : nonOrderedPostProcessorNames) {
        BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
        nonOrderedPostProcessors.add(pp);
        if (pp instanceof MergedBeanDefinitionPostProcessor) {
            internalPostProcessors.add(pp);
        }
    }
    registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

    // Finally, re-register all internal BeanPostProcessors.
    sortPostProcessors(internalPostProcessors, beanFactory);
    registerBeanPostProcessors(beanFactory, internalPostProcessors);

    // Re-register post-processor for detecting inner beans as ApplicationListeners,
    // moving it to the end of the processor chain (for picking up proxies etc).
    beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM