詳解Spring ApplicationContext加載過程

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));
 }

以上就是詳解Spring ApplicationContext加載過程的詳細內容,更多關於Spring ApplicationContext加載過程的資料請關註WalkonNet其它相關文章!

推薦閱讀: