首页 > Spring上下文刷新第五步
头像
归墨
编辑于 2021-04-21 18:22
+ 关注

Spring上下文刷新第五步

该步骤执行于上下文刷新时BeanFactory基础属性设置完毕后.第563行invokeBeanFactoryPostProcessors方法中.
在此步Spring扫描加载完毕所有的BeanDefinition.然后执行所有postProcessBeanDefinitionRegistry方法和postProcessBeanFactory方法

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

    Set<String> processedBeans = new HashSet<>();
    // 如果BeanFactory刚好也是BeanDefinitionRegistry,则先执行BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry方法
    if (beanFactory instanceof BeanDefinitionRegistry) {
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
        // 用于执行BeanFactoryPostProcessor.postProcessBeanFactory
        List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
        // 用于执行BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry
        List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

        // 如果postProcessor是BeanDefinitionRegistryPostProcessor则先执行postProcessBeanDefinitionRegistry方法
        for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
            if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                BeanDefinitionRegistryPostProcessor registryProcessor =
                        (BeanDefinitionRegistryPostProcessor) postProcessor;
                registryProcessor.postProcessBeanDefinitionRegistry(registry);
                registryProcessors.add(registryProcessor);
            }
            else {
                regularPostProcessors.add(postProcessor);
            }
        }

        // 按照不同排序优先级区分开不同的BeanDefinitionRegistryPostProcessor
        List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

        // 先执行实现PriorityOrdered优先级接口的BeanDefinitionRegistryPostProcessor
        // 从Bean工厂中获取所有BeanDefinitionRegistryPostProcessor的Bean名称(未初始化的)
        String[] postProcessorNames =
                beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
        for (String ppName : postProcessorNames) {
            if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                // 执行过的会加入到这里面中进行重复验证防止重复执行
                processedBeans.add(ppName);
            }
        }
        // 排序PriorityOrdered.class,越小越大
        sortPostProcessors(currentRegistryProcessors, beanFactory);
        // 和从上下文传进来的默认Processor合并
        registryProcessors.addAll(currentRegistryProcessors);
        // 执行这些BeanPostProcessor,在这步之前BeanFactory中还只是有几个一开始初始化添加进去的系统Bean.这步会将所有定义的BeanDefinition扫描注册到BeanFactory中.这里会执行一个系统级的ConfigurationClassPostProcessor的postProcessBeanDefinitionRegistry方法.来将所有BeanDefinition扫描到容器中.可打断点查看
        invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
        currentRegistryProcessors.clear();

        // 执行实现Orderd优先级接口的BeanDefinitionRegistryPostProcessor
        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, beanFactory.getApplicationStartup());
        currentRegistryProcessors.clear();

        // 最后执行其他无要求的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, beanFactory.getApplicationStartup());
            currentRegistryProcessors.clear();
        }

        // 最后执行所有的BeanPostProcessor(BeanDefinitionRegistryPostProcessor实现了BeanFactoryPostProcessor接口)
        invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
        invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
    }

    else {
        // 只执行默认的BeanFactoryPostProcessor
        invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
    }

    // 执行剩下的未加载为Bean的BeanFactoryProstProcessors.(自定义的)
    String[] postProcessorNames =
            beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

    // 和上面按照优先级分段执行方式如出一辙,但是更加直观清晰
    // 先执行PriorityOrdered优先级的,再执行Ordered优先级的,最后执行无优先级的
    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<>(orderedPostProcessorNames.size());
    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<>(nonOrderedPostProcessorNames.size());
    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();
}

BeanDefinition加载前
component标注的BeanDefinition加载
配置类中获取的BeanDefinition加载

全部评论

(0) 回帖
加载中...
话题 回帖

近期热帖

近期精华帖

热门推荐