美文网首页探索Spring
Spring Bean生命周期-invokeBeanFactor

Spring Bean生命周期-invokeBeanFactor

作者: Real_man | 来源:发表于2018-10-09 08:26 被阅读4次

    在beanFactory准备好之后,(配置ignoreDependecy,ResolvableDependency,PostProcessors),现在要调用BeanFactoryPostProcessors了。

    实例化然后调用所有注册的BeanFactoryPostProcessor Bean。必须在单例(singleton)实例化之前调用。

    可以看到这个方法使用的是PostProcessorRegistrationDelegate的静态方法,getBeanFactoryPostProcessors(),获取的是BeanFactoryPostProcessors,默认为空,如果需要我们可以在postProcessBeanFactory方法中指定,因为这个方法默认也是没有实现的。

    这里也可以发现:ApplicationContext是对beanFactory的封装,其内部包含了对beanFactory的前置处理还有多种附加功能。
    而BeanFactory针对的是Bean的处理。

    protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
            PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
        }
    

    分析

    这里面的一段代码实在是有点长,分开一部分一部分的看。

        public static void invokeBeanFactoryPostProcessors(
                ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
    
            // Invoke BeanDefinitionRegistryPostProcessors first, if any.
            Set<String> processedBeans = new HashSet<String>();
         
          // beanFactory默认使用的是DefaultListableBeanFactory,因此是BeanDefinitionRegistry的实例
            if (beanFactory instanceof BeanDefinitionRegistry) {
                BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
                
                //准备两种PostProcessors
                List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<BeanFactoryPostProcessor>();
                List<BeanDefinitionRegistryPostProcessor> registryPostProcessors =
                        new LinkedList<BeanDefinitionRegistryPostProcessor>();
    
             //将传进来的beanFactoryPostProcessors分成两种类型,如果是BeanDefinitionRegistryPostProcessor类型的还会进行postProcess一下,其它类型的先添加进regularPostProcessors列表中
                for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
                    if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                        BeanDefinitionRegistryPostProcessor registryPostProcessor =
                                (BeanDefinitionRegistryPostProcessor) postProcessor;
                        registryPostProcessor.postProcessBeanDefinitionRegistry(registry);
                        registryPostProcessors.add(registryPostProcessor);
                    }
                    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.
                // 先不要实例化FactoryBeans,我们需要等到bean被factory post-processors处理。
                //分类BeanDefinitionRegistryPostProcessors, 按照实现PriorityOrdered,Ordered等分类。
                String[] postProcessorNames =
                        beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
    
                // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
                List<BeanDefinitionRegistryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>();
                for (String ppName : postProcessorNames) {
                    if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                        priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                        processedBeans.add(ppName);
                    }
                }
                
                // 排序,普通的集合排序,然后安装顺序调用BeanDefinitionRegistryPostProcessor
                sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
                registryPostProcessors.addAll(priorityOrderedPostProcessors);
                invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry);
    
                // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
                // 处理实现Ordered的BeanDefinitionRegistryPostProcessor
                postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
                List<BeanDefinitionRegistryPostProcessor> orderedPostProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>();
                for (String ppName : postProcessorNames) {
                    if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
                        orderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                        processedBeans.add(ppName);
                    }
                }
                sortPostProcessors(beanFactory, orderedPostProcessors);
                registryPostProcessors.addAll(orderedPostProcessors);
                invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry);
    
                // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
                //处理不被processedBeans包含的Bean
                boolean reiterate = true;
                while (reiterate) {
                    reiterate = false;
                    postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
                    for (String ppName : postProcessorNames) {
                        if (!processedBeans.contains(ppName)) {
                            BeanDefinitionRegistryPostProcessor pp = beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class);
                            registryPostProcessors.add(pp);
                            processedBeans.add(ppName);
                            pp.postProcessBeanDefinitionRegistry(registry);
                            reiterate = true;
                        }
                    }
                }
    
                // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
                // 所有的目前处理的processors,调用postProcessBeanFactory的回调
                invokeBeanFactoryPostProcessors(registryPostProcessors, 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!
            //不要初始化FactoryBeans,我们需要让Bean factory post-processors处理普通的bean。
            String[] postProcessorNames =
                    beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
    
            // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
            // Ordered, and the rest.
            List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
            List<String> orderedPostProcessorNames = new ArrayList<String>();
            List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
            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.
            sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
            invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
    
            // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
            List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
            for (String postProcessorName : orderedPostProcessorNames) {
                orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
            }
            sortPostProcessors(beanFactory, orderedPostProcessors);
            invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
    
            // Finally, invoke all other BeanFactoryPostProcessors.
            List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
            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();
        }
    
    

    代码看起来很长,不过大部分的代码处理逻辑类似,细看的话不复杂。
    主要是

    • beanFactoryPostProcessors分成BeanDefinitionRegistryPostProcessor与regularPostProcessors。BeanDefinitionRegistryPostProcessor在划分的时候也会调用下postProcessBeanDefinitionRegistry(registry)
    • BeanDefinitionRegistryPostProcessor分成实现了PriorityOrdered,Ordered还有其他的统一处理,分类之后调用postProcessBeanDefinitionRegistry(registry)方法
    • BeanFactoryPostProcessor分成实现PriorityOrdered,Ordered等接口的类型,然后统一调用postProcessBeanFactory方法
    • 清空cache

    默认情况下,ApplicationContext中的beanFactoryPostProcessors是需要我们自定义的,这需要我们对beanFactoryPostProcessors有较深的理解,后面可以研究下beanFactoryPostProcessors

    TODO

    • beanFactoryPostProcessors用法,demo

    最后

    这次一起看了invokeBeanFactoryPostProcessors方法,这是实例化Factory之前预处理部分,更深的理解还要坚持看下去理解。

    相关文章

      网友评论

        本文标题:Spring Bean生命周期-invokeBeanFactor

        本文链接:https://www.haomeiwen.com/subject/dqasaftx.html