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