Spring 源码之AbstractApplicationContext#invokeBeanFactoryPostProcessors

AbstractApplicationContext#invokeBeanFactoryPostProcessors调用PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors

实例化并调用所有已注册的BeanFactoryPostProcessorBean

   
   // AbstractApplicationContext
   protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
		PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
                 // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
                // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
                // 这里debug没执行暂时不知道 , 感觉注册bean后处理器
		if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
			beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
			beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
		}
	}
    
   // PostProcessorRegistrationDelegate
   public static void invokeBeanFactoryPostProcessors(
                ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

            // Invoke BeanDefinitionRegistryPostProcessors first, if any.
            // 保存所有已被调用过的后处理器, 避免重复调用
            Set<String> processedBeans = new HashSet<>();

            if (beanFactory instanceof BeanDefinitionRegistry) {
                // SpringContext容器上下文持有的容器对象, 将它转为BeanDefinitionRegistry接口实现类
                // BeanDefinitionRegistry接口的主要是提供BeanDefinition管理注册
                BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
                List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
                List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

                // for循环内部的if判断是为了区分BeanDefinitionRegistryPostProcessor和beanFactoryPostProcessor接口
                // BeanDefinitionRegistryPostProcessor继承自beanFactoryPostProcessor
                // BeanDefinitionRegistryPostProcessor在容器初始化内部bean定义都注册后修改, 可以在后处理之前添加BeanDefinition
                // 会先调用BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry
                for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) { 
                    if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                        BeanDefinitionRegistryPostProcessor registryProcessor =
                                (BeanDefinitionRegistryPostProcessor) postProcessor;
                        registryProcessor.postProcessBeanDefinitionRegistry(registry);
                        registryProcessors.add(registryProcessor);
                    }
                    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.
                List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

                // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
                // beanFactory.getBeanNamesForType这方法会获取符合类型的beanDefinition并进行实例化
                // 此方法后面的代码和这里类似就不用解释了
                String[] postProcessorNames =
                        beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
                for (String ppName : postProcessorNames) {
                    // 查看当前遍历的后处理对象是否实现了PriorityOrdered接口, 实现了了这个接口会优先调用      
                    // BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry方法
                    if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                        currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                        processedBeans.add(ppName);
                    }
                }
                sortPostProcessors(currentRegistryProcessors, beanFactory);
                registryProcessors.addAll(currentRegistryProcessors);
                invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
                currentRegistryProcessors.clear();

                // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
                postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
                for (String ppName : postProcessorNames) {
                    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.
                // 调用所有剩下的BeanDefinitionRegistryPostProcessor
                boolean reiterate = true;
                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);
                    invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
                    currentRegistryProcessors.clear();
                }

                // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
                invokeBeanFactoryPostProcessors(registryProcessors, 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!
            // 这里开始获取BeanFactoryPostProcessor接口的实现类
            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) {
                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(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));
            }
            // 最后调用所有还未调用BeanFactoryPostProcessor.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...
            // 因为工厂后处理可能修改beanDefinition, 所以要将对象工厂的元数据缓存进行清空
            beanFactory.clearMetadataCache();
        }

 
总结:

  • AbstractApplicationContext#refresh->invokeBeanFactoryPostProcessors->PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors
    在PPRD.iBFP中实现了对所有实现了BeanFactoryPostProcessor#postProcessBeanDefinitionRegistry和实现了BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry接口的方法的调用, 而BeanDefinitionRegistryPostProcessor又继承自BeanFactoryPostProcessor.
  • 具体的调用逻辑:
    1. 调用实现了BDRPP接口的后处理器, 这里面又优先调用从方法入口参数传入的后处理器, 因为入口参数传入的后处理器都是已经实例化了
    2. 从beanFactory工厂的beanDefinition中获取实现了BDRPP接口创建实例的bean进行实例化然后调用.
    3. 对于第二步还有内部调用顺序, 优先调用同时实现了PriorityOrdered或者Ordered接口的后处理器类, ProrityOrdered优先级高于Ordered. 这两个接口的实现类调用完毕之后调用普通的BDRPP接口实现类
    4. 这时候就开始调用直接实现BeanFactoryPostProcessor的后处理器方法, 这里顺序和第三步类似, 都对排序相关接口做了处理.
    5. 最后beanFactory.clearMetadataCache()清理工厂缓存.
  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值