乐趣区

关于spring:四Spring-ApplicationContext加载过程

Spring Bean 加载过程

1、找准入口,应用 ClassPathXmlApplicationContext 的构造方法加载配置文件,用于加载 classPath 下的配置文件

// 第一行,执行实现之后就实现了 spring 配置文件的加载,刷新 spring 上下文
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring-mvc.xml");
// 获取实例 Bean
Person person=context.getBean("person",Person.class);

2、ClassPathXmlApplicationContext 构造方法源码如下:

public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
            throws BeansException {
        // 设置父级的 ApplicationContext,null
        super(parent);
        //1. 设置配置文件的门路,2. 将门路中的占位符 ${placeholder} 应用零碎的变量替换
        setConfigLocations(configLocations);
        if (refresh) {refresh();
        }
    }

3、次要办法为 setConfigLocation(configLocation), 这个办法调用其父类 AbstractRefreshableConfigApplicationContext 中的办法

//locations : 配置文件门路
public void setConfigLocations(String[] locations) {if (locations != null) {
            // 断言
            Assert.noNullElements(locations, "Config locations must not be null");
            // 存储配置文件门路的数组,存储去掉占位符后的文件门路数组
            this.configLocations = new String[locations.length];
            // 遍历 locations,解析占位符
            for (int i = 0; i < locations.length; i++) {
                    // 调用 resolvePath 解析占位符
                this.configLocations[i] = resolvePath(locations[i]).trim();}
        }
        else {this.configLocations = null;}
    }

4、进入 resovePath 的源码,实际上执行的是 AbstractPropertyResolver 的 doResolverPlaceholders 办法

/**
* text : 须要解析的门路
* PropertyPlaceholderHelper:这个是解析零碎占位符的辅助类,次要用来将占位符替换成零碎的环境变量
*/
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {
        // 调用 PropertyPlaceholderHelper 类中的 replacePlaceholders 办法
        return helper.replacePlaceholders(text, new PropertyPlaceholderHelper.PlaceholderResolver() {public String resolvePlaceholder(String placeholderName) {return getPropertyAsRawString(placeholderName);
            }
        });
    }

5、进入 PropertyHelper 的 replacePlaceholder 办法,实际上调用 PropertyPlaceholderHelper 的 parseStringValue 解析占位符

public String replacePlaceholders(String value, PlaceholderResolver placeholderResolver) {Assert.notNull(value, "Argument'value'must not be null.");
        // 调用的是 parseStringValue 办法
        return parseStringValue(value, placeholderResolver, new HashSet<String>());
    }
 
/**
* strVal:须要解析的字符串,就是配置文件的门路
* placeholderResolver:策略接口,占位符解析器
* visitedPlaceholders:存储曾经拜访过的占位符
**/
protected String parseStringValue(String strVal, PlaceholderResolver placeholderResolver, Set<String> visitedPlaceholders) {
        // 将 strval 转换成 StringBuilder,便于后续到操作
        StringBuilder buf = new StringBuilder(strVal);
    
    //this.placeholderPrefix 这个是占位符的前缀 ${, 在创立 PropertyHelper 的时候就曾经指定了占位符的 placeholderPrefix="${" ,placeholderSuffix="}",valueSeparator=":"
    // 获取前缀在这个配置文件门路中的开始索引    
        int startIndex = strVal.indexOf(this.placeholderPrefix);
    
        while (startIndex != -1) {
            // 占位符前缀在门路中的完结索引
            int endIndex = findPlaceholderEndIndex(buf, startIndex);
            
            // 如果完结索引存在
            if (endIndex != -1) {// 此时取出 ${plcaeholder} 中的占位符内容 placeholder
                String placeholder = buf.substring(startIndex + this.placeholderPrefix.length(), endIndex);
                
                // 保留取出来的占位符内容 placeholder
                String originalPlaceholder = placeholder;
                
                // 如果占位符中的内容曾经被拜访过了,抛出出异样返回,递归完结的条件
                if (!visitedPlaceholders.add(originalPlaceholder)) {
                    throw new IllegalArgumentException("Circular placeholder reference'" + originalPlaceholder + "'in property definitions");
                }
                
                // 递归解析曾经取出的占位符中的内容 palceholder
                placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
                
                
                // 这个最重要的一步,将解析占位符内容 placeholder 的值,比方将 java.version 转换成 1.8.0_60
                String propVal = placeholderResolver.resolvePlaceholder(placeholder);
                
                if (propVal == null && this.valueSeparator != null) {int separatorIndex = placeholder.indexOf(this.valueSeparator);
                    if (separatorIndex != -1) {String actualPlaceholder = placeholder.substring(0, separatorIndex);
                        String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length());
                        propVal = placeholderResolver.resolvePlaceholder(actualPlaceholder);
                        if (propVal == null) {propVal = defaultValue;}
                    }
                }
                // 如果解析进去的占位符不为空,比方 ${java.version} 将被解析成 1.8.0_60
                if (propVal != null) {
                    // 此时持续递归解析出 1.8.0_60 中的占位符
                    propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders);
                    // 将门路中的占位符替换成零碎变量的值,比方将 ${java.version} 替换成 1.8.0_60
                    buf.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
                    if (logger.isTraceEnabled()) {logger.trace("Resolved placeholder'" + placeholder + "'");
                    }
                    // 持续在门路字符串中残余的子串中查找占位符,如果有占位符,那么还会持续解析占位符
                    startIndex = buf.indexOf(this.placeholderPrefix, startIndex + propVal.length());
                }
                else if (this.ignoreUnresolvablePlaceholders) {
                    // Proceed with unprocessed value.
                    startIndex = buf.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
                }
                else {
                    throw new IllegalArgumentException("Could not resolve placeholder'" +
                            placeholder + "'"+" in string value \""+ strVal +"\"");
                }
                // 将已转换胜利的占位符从以拜访的汇合中移除即可
                visitedPlaceholders.remove(originalPlaceholder);
            }
            else {startIndex = -1;}
        }
 
        return buf.toString();   // 将解析实现之后的配置文件返回}

6、而后是 ClassPathXmlApplicationContext 中的 refresh 办法,实际上调用的是父类 AbstractApplicationContext 的办法

// 刷新 spring 上下文
public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {
            // 在刷新之前设置一些参数,比方设置开始工夫戳,上下文是否激活的标记,输入刷新上下文的信息,验证一些必要的属性
            prepareRefresh();
 
            // 须要创立 beanFactory,如果曾经存在 beanFactory,那么敞开,具体其请看 10
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
 
            // 筹备上下文工厂,详情见 12
            prepareBeanFactory(beanFactory);
 
            try {
                // 容许子类向后置处理器增加组件
                postProcessBeanFactory(beanFactory);
 
                // 调用 BeanFactoryPostProcessor 和 BeanDefintionRegistoryPostProcessor 这两个后置处理器
                invokeBeanFactoryPostProcessors(beanFactory);
 
                // 注册 BeanPostProcessor, 用来拦挡 bean 的创立,详情见 14
                registerBeanPostProcessors(beanFactory);
 
                // 初始化音讯源
                initMessageSource();
 
                // 初始化应用程序事件播送器,用户能够自定义一个事件播送器,如果用户没有定义,那么应用默认的事件播送器 SimpleApplicationEventMulticaster
                initApplicationEventMulticaster();
 
                // 在其余子类中初始化 bean
                onRefresh();
 
                // 检测事件监听器
                registerListeners();
 
                // 实现实例化残余的单例 (non-lazy-init)
                finishBeanFactoryInitialization(beanFactory);
 
                // 实现刷新,初始化生命周期处理器......
                finishRefresh();}
 
            catch (BeansException ex) {
                // Destroy already created singletons to avoid dangling resources.
                destroyBeans();
 
                // Reset 'active' flag.
                cancelRefresh(ex);
 
                // Propagate exception to caller.
                throw ex;
            }
        }
    }

7、进入 obtainFreshBeanFactory 办法

//AbastractApplicationContext 的办法
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {// 理论刷新上下文的办法,这个办法就是理论的刷新上下文办法,其中会调用 loadBeanDefinitions(beanFactory); 加载配置文件中的内容到 BeanDefiniton 中
        refreshBeanFactory();
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();
        if (logger.isDebugEnabled()) {logger.debug("Bean factory for" + getDisplayName() + ":" + beanFactory);
        }
        return beanFactory;
    }
 
 
    //org.springframework.context.support.AbstractRefreshableApplicationContext 中的办法
    //AbstractApplicationContext 的子类中的办法
    @Override
    protected final void refreshBeanFactory() throws BeansException {
        // 如果其中有 beanfactory,那么销毁
        if (hasBeanFactory()) {destroyBeans();
            closeBeanFactory();}
        
        try {
            // 从新创立一个 beanFactory
            DefaultListableBeanFactory beanFactory = createBeanFactory();
            // 设置序列化 id
            beanFactory.setSerializationId(getId());
            
            // 定制 beanFactory,设置相干属性,包含是否容许笼罩名称的不同定义的对象及循环依赖以及
            // 设置 @Autowired 和 @Qualifier,注解解析器 QualifierAnnotationAutowireCandidateResolver
            customizeBeanFactory(beanFactory);
            // 加载 BeanDefine 详情见  11
            loadBeanDefinitions(beanFactory);
            synchronized (this.beanFactoryMonitor) {this.beanFactory = beanFactory;}
        }
        catch (IOException ex) {throw new ApplicationContextException("I/O error parsing bean definition source for" + getDisplayName(), ex);
        }
    }

8、进入 loadBeanDefinitions(beanFactory) 办法

    // 这个是 org.springframework.context.support.AbstractXmlApplicationContext 类中的办法
    protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
        
        // 创立要给 beanDefinitionReader,用于读取 BeanDefinition
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
 
        // 配置 XmlBeanDefinitionReader
        beanDefinitionReader.setEnvironment(this.getEnvironment());
        beanDefinitionReader.setResourceLoader(this);   
        beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
    
        initBeanDefinitionReader(beanDefinitionReader);
        // 加载 BeanDefiniton,次要的性能从配置文件中读取 BeanDefiniton 注册到注册表中
        loadBeanDefinitions(beanDefinitionReader);
    }

9、prepareBeanFactory: 筹备 BeanFactory

// 筹备 BeanFactory,设置一些参数,比方后置处理器,protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
        // 设置类加载器
        beanFactory.setBeanClassLoader(getClassLoader());
        
    // 设置表达式解析器,用来解析 BeanDefiniton 中的带有表达式的值
        beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
        
    
        beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
 
        // 配置后置处理器,次要的作用就是在 spring 实例化 bean 的前后做一些操作
        beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
        
        // 疏忽主动拆卸的类,这些类都不能应用 @Resource 或者 @Autowired 主动拆卸获取对象
        beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
        beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
        beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
        beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
        beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
        beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
 
        // 注册可解析的主动拆卸类
        beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
        beanFactory.registerResolvableDependency(ResourceLoader.class, this);
        beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
        beanFactory.registerResolvableDependency(ApplicationContext.class, this);
 
        // 在增加一个应用程序监听器
        beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
 
        // 查看这些类是否被
        if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
 
            beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
        }
 
        // 将上面这些类注册到容器中,应用 registerSingleton 办法注册,咱们能够间接从容器中获取这些类的对象应用
        if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
        }
        if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
        }
        if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
        }
    }

10、调用 BeanFactory 的后置处理器,次要的性能就是调用注册在容器中的 BeanFactoryPostProcessor 和 BeanDefinitionRegistoryPostProcessor

// 实例化和调用 BeanFactory 后置处理器,必须在单例实例化之前调用
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        // 调用后置处理器注册委托类的办法调用,getBeanFactoryPostProcessors 用于获取注册的全副的 BeanFactoryPostProcessor
        PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
    }
 
// 理论的调用办法,PostProcessorRegistrationDelegate 中的办法
public static void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
 
        // Invoke BeanDefinitionRegistryPostProcessors first, if any.
        Set<String> processedBeans = new HashSet<String>();
        
        // 如果 beanFactory 是 BeanDefinitionRegistry 的子类,BeanDefinitionRegistry 应用来向注册表中注册 Bean 的元信息的 (BeanDefintion)
        if (beanFactory instanceof BeanDefinitionRegistry) {BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
            
            // 寄存 BeanFactoryPostProcessor
            List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<BeanFactoryPostProcessor>();
            
            // 寄存 BeanDefinitionRegistryPostProcessor
            List<BeanDefinitionRegistryPostProcessor> registryPostProcessors =
                    new LinkedList<BeanDefinitionRegistryPostProcessor>();
            
            // 遍历。判断是否是 BeanDefinitionRegistryPostProcessor 实例
            for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                    BeanDefinitionRegistryPostProcessor registryPostProcessor =
                            (BeanDefinitionRegistryPostProcessor) postProcessor;
                    
                        // 调用 BeanDefinitionRegistryPostProcessor
                    registryPostProcessor.postProcessBeanDefinitionRegistry(registry);
                    // 增加
                    registryPostProcessors.add(registryPostProcessor);
                }
                else {
                    // 示意这个是 BeanFactoryPostProcessor 实例,增加进汇合
                    regularPostProcessors.add(postProcessor);
                }
            }
 
            //--- 依据类型类型获取 beanFactory 中注册的 BeanDefinitionRegistryPostProcessor 的 bean 的所有名称数组
            String[] postProcessorNames =
                    beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 
            // ---- 首先调用的是 BeanDefinitionRegistryPostProcessor 类型的后置处理器
            
            // 寄存实现 PriorityOrdered 这个接口的 BeanDefinitionRegistryPostProcessor
            List<BeanDefinitionRegistryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>();
            
            // 遍历,如果实现了 PriorityOrdered 这个接口就保留下来
            for (String ppName : postProcessorNames) {if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    processedBeans.add(ppName);
                }
            }
            
            // 依照优先级排序
            OrderComparator.sort(priorityOrderedPostProcessors);
            // 增加进入汇合
            registryPostProcessors.addAll(priorityOrderedPostProcessors);
            
            // 首先调用实现 PriorityOrdered 这个接口的 BeanDefinitionRegistryPostProcessor
            invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry);
 
            // ---- 上面是调用实现 Orderd 这个接口的 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);
                }
            }
            OrderComparator.sort(orderedPostProcessors);
            registryPostProcessors.addAll(orderedPostProcessors);
            invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry);
 
            
            // ---- 最终调用残余全副的 BeanDefinitionRegistryPostProcessor
            
            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;
                    }
                }
            }
 
            // 调用 BeanFactoryPostProcessor 接口中的办法,因为 BeanDefitionRegistory 继承了这个接口
            invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory);
            invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
        }
 
        else {
            // Invoke factory processors registered with the context instance.
            invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
        }
 
        //--- 上面是调用实现 BeanFactoryPostProcessor 接口的类,和下面的流程一样
        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.
        OrderComparator.sort(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));
        }
        OrderComparator.sort(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);
    }

11、注册 BeanPostProcessor,用来拦挡 Bean 的创立,这个接口能够实现在 Bean 初始化和初始化之后执行相干的操作

// 仍然这里仍然调用的 PostProcessorRegistrationDelegate,其中蕴含了注册后置处理器和调用后置处理器的办法,相当于一个代理人
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
    }
 
//PostProcessorRegistrationDelegate 中的注册 BeanPostProcessors 的办法
// 其中 beanFactory 这个新创建的 beanFactory,其中的 BeanPostProcessor 都没有注册,applicationContext 这个是之前创立的,其中的处理器曾经注册过了
public static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
        
        // 依据类型新加载全副的 BeanFactoryProcessor 的类,String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
 
        // 创立 BeanPostProcessor 检测器
        int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
        beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
 
        // Separate between BeanPostProcessors that implement PriorityOrdered,
        // Ordered, and the rest.
        List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
        List<BeanPostProcessor> internalPostProcessors = new ArrayList<BeanPostProcessor>();
        List<String> orderedPostProcessorNames = new ArrayList<String>();
        List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
        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);
            }
        }
 
        // First, register the BeanPostProcessors that implement PriorityOrdered.
        OrderComparator.sort(priorityOrderedPostProcessors);
        registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
 
        // Next, register the BeanPostProcessors that implement Ordered.
        List<BeanPostProcessor> orderedPostProcessors = new ArrayList<BeanPostProcessor>();
        for (String ppName : orderedPostProcessorNames) {BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
            orderedPostProcessors.add(pp);
            if (pp instanceof MergedBeanDefinitionPostProcessor) {internalPostProcessors.add(pp);
            }
        }
        OrderComparator.sort(orderedPostProcessors);
        registerBeanPostProcessors(beanFactory, orderedPostProcessors);
 
        // Now, register all regular BeanPostProcessors.
        List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
        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.
        OrderComparator.sort(internalPostProcessors);
        registerBeanPostProcessors(beanFactory, internalPostProcessors);
 
        beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
    }
退出移动版