spring原始碼閱讀--容器啟動過程

林學習發表於2018-10-22
      最近把spring又拿起來看了一下,以下是一些閱讀筆記(多點耐心,往下看完)。
      首先選用ClassPathXmlApplicationContext作為容器。不多說,直接上程式碼      

package com.example.demo.springcontext;

import com.example.demo.beanfactory.Car;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author <a href="mailto:wujia@2dfire.com">linxiaohui</a>
 * @version 1.0 18/10/11
 * @since 1.0
 */
public class ApplicationContextTest {

    public static void main(String[] args) {
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        Car car = ctx.getBean("car", Car.class);
        car.introduce();
    }
}
複製程式碼

     ClassPathXmlApplicationContext的建構函式

public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
   this(new String[] {configLocation}, true, null);
}
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
      throws BeansException {

   super(parent);
   //對傳入的配置檔名稱進行解析,比如檔名稱中使用了佔位符的,那就需要替換成真實的值
   setConfigLocations(configLocations);
   if (refresh) {
      //呼叫容器的啟動方法
      refresh();
   }
}複製程式碼

      下面我們先來整體看下spring容器啟動的步驟,然後再逐步分析下每一步都幹了些什麼事情。

public void refresh() throws BeansException, IllegalStateException {
   synchronized (this.startupShutdownMonitor) {
      // 1、容器啟動前的準備工作
      prepareRefresh();

      // 2、初始化beanFactory,載入配置檔案,轉換為beanDefinition,並註冊這些beanDefinition
      ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

      // 3、設定beanFactory的一些屬性,增強功能
      prepareBeanFactory(beanFactory);

      try {
         // 4、完成步驟3後,可以對beanFactory做一些後置處理。由子類擴充套件實現
         postProcessBeanFactory(beanFactory);

         // 5、呼叫容器中註冊的beanFactory後置處理方法
         invokeBeanFactoryPostProcessors(beanFactory);

         // 6、註冊bean的後置處理器。(tips:觸發時機是bean例項化後,在bean初始化的前後。)
         registerBeanPostProcessors(beanFactory);

         // 7、初始化國際化用到的資源
         initMessageSource();

         // 8、初始化事件廣播器
         initApplicationEventMulticaster();

         // 9、初始化其他特殊的bean,由特定的子類去實現
         onRefresh();

         // 10、註冊事件監聽器,釋出需要提前廣播的事件
         registerListeners();

         // 11、例項化剩下的非延遲載入的單例
         finishBeanFactoryInitialization(beanFactory);

         // 12、完成容器啟動,處理後置事件以及釋出通知事件
         finishRefresh();
      }

      catch (BeansException ex) {
         if (logger.isWarnEnabled()) {
            logger.warn("Exception encountered during context initialization - " +
                  "cancelling refresh attempt: " + ex);
         }

         // Destroy already created singletons to avoid dangling resources.
         destroyBeans();

         // Reset 'active' flag.
         cancelRefresh(ex);

         // Propagate exception to caller.
         throw ex;
      }

      finally {
         // Reset common introspection caches in Spring's core, since we
         // might not ever need metadata for singleton beans anymore...
         resetCommonCaches();
      }
   }
}複製程式碼


step1、容器啟動前的準備工作

/**
 * Prepare this context for refreshing, setting its startup date and
 * active flag as well as performing any initialization of property sources.
 */
protected void prepareRefresh() {
   //設定啟動時間
   this.startupDate = System.currentTimeMillis();
   //設定容器狀態
   this.closed.set(false);
   this.active.set(true);

   if (logger.isInfoEnabled()) {
      logger.info("Refreshing " + this);
   }

   // 初始化檔案,由子類實現
   initPropertySources();

   // 驗證必須要的屬性是否已經存在
   // see ConfigurablePropertyResolver#setRequiredProperties
   getEnvironment().validateRequiredProperties();

   // 儲存容器需要提前廣播的事件
   // tips:這些提前事件會在registerListeners()註冊事件監聽器的時候直接廣播
   this.earlyApplicationEvents = new LinkedHashSet<ApplicationEvent>();
}複製程式碼

    第1步主要做一些容器啟動前的狀態設定,以及必須屬性的校驗。


step2、初始化beanFactory,載入配置檔案

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
   //主要看這個方法程式碼
   refreshBeanFactory();
   ConfigurableListableBeanFactory beanFactory = getBeanFactory();
   if (logger.isDebugEnabled()) {
      logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
   }
   return beanFactory;
}

protected final void refreshBeanFactory() throws BeansException {
   //已經存在beanFactory
   if (hasBeanFactory()) {
      //清理所有的單例快取
      destroyBeans();
      //設定beanFactory為null
      closeBeanFactory();
   }
   try {
      //使用DefaultListableBeanFactory建立beanFactory例項
      DefaultListableBeanFactory beanFactory = createBeanFactory();
      //設定序列化id
      beanFactory.setSerializationId(getId());
      //自定義屬性設定:1、是否允許同名bean(後者覆蓋前者)  2、是否允許迴圈依賴
      customizeBeanFactory(beanFactory);
      //載入解析xml檔案配置,轉化為記憶體中的beanDefinition格式,並註冊到對應的集合列表當中
      loadBeanDefinitions(beanFactory);
      synchronized (this.beanFactoryMonitor) {
         this.beanFactory = beanFactory;
      }
   }
   catch (IOException ex) {
      throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
   }
}
複製程式碼

      第2步主要是新建立一個beanFactory,設定序列化id,以及自定義屬性,然後解析xml配置的bean資訊備用。

step3、設定beanFactory的一些屬性,擴充套件功能

protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
   // 設定AppClassLoader作為類載入器
   beanFactory.setBeanClassLoader(getClassLoader());
   //設定spring EL 表示式解析器
   beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
   //設定屬性編輯器
   beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

   // 設定bean後置處理器,讓我們可以獲取整個容器,同時會注入資源。(EnvironmentAware,EmbeddedValueResolverAware
   //,ResourceLoaderAware,ApplicationEventPublisherAware,MessageSourceAware,ApplicationContextAware)
   //在bean例項化的時候會用到。(後續文章會分析preInstantiateSingletons()->getBean() ->
   //doGetBean()->createBean()->doCreateBean()->initializeBean()->invokeAwareMethods())   
   beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
   //忽略上步註冊後置處理器自動注入的資源
   beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
   beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
   beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
   beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
   beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
   beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

   // 註冊spring自己的系統bean,和業務bean區分
   beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
   beanFactory.registerResolvableDependency(ResourceLoader.class, this);
   beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
   beanFactory.registerResolvableDependency(ApplicationContext.class, this);

   // 註冊ApplicationListenerDarecror後置處理器,把實現了ApplicationListener介面的單例物件
   // 註冊到容器的事件集合中去
   beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

   // AOP支援。此處不擴充套件,後續AOP模組再分析
   if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
      beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
      // Set a temporary ClassLoader for type matching.
      beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
   }

   // 註冊預設的系統環境bean。(具體內容可以打斷點檢視,比如佔位符字首'${',字尾'}',值的分隔符':'等等)
   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());
   }
}複製程式碼


step4、完成步驟3後對beanFactory做一些後置處理。由子類擴充套件

/**
 * Modify the application context's internal bean factory after its standard
 * initialization. All bean definitions will have been loaded, but no beans
 * will have been instantiated yet. This allows for registering special
 * BeanPostProcessors etc in certain ApplicationContext implementations.
 * @param beanFactory the bean factory used by the application context
 */
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    //在beanFactory載入解析xml配置檔案後,bean例項化之前,子類可以在這個時期做一些特殊的處理,對
    //beanFactory進行屬性增強(可參考不同容器子類的具體實現)
}複製程式碼


step5、呼叫容器中註冊的beanFactory後置處理器的方法

      在此處先穿插一個知識點--spring容器擴充套件點。我們在這裡主要簡單講下BeanPostProcessor和BeanFactoryPostProcessor。

      BeanPostProcessor是在spring容器載入了bean的定義檔案並且例項化bean之後執行的,分別為bean呼叫初始化init-method的前後。(操作目標物件為bean物件)

      BeanFactoryPostProcessor是在spring容器載入了bean的定義檔案(xml檔案)之後,在bean例項化之前執行的。(操作目標物件為beanFactory物件)

      我們知道了兩者的執行時機和使用目的,就可以繼續往下看了。至於兩者的詳細使用,此處不擴充套件開了。

protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
   //核心程式碼,看下文註釋
   PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());

   // AOP支援。此處不擴充套件,後續AOP模組再分析
   if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
      beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
      beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
   }
}
複製程式碼

//看此處程式碼之前,首先得知道,BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子介面,子介面
//擴充套件了父介面,所以下面對兩種處理器分開執行
public static void invokeBeanFactoryPostProcessors(
      ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

   // Invoke BeanDefinitionRegistryPostProcessors first, if any.
   Set<String> processedBeans = new HashSet<String>();
   //是否BeanDefinitionRegistry型別
   if (beanFactory instanceof BeanDefinitionRegistry) {
      BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
      //儲存BeanFactoryPostProcessor處理器
      List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<BeanFactoryPostProcessor>();
      //儲存BeanDefinitionRegistryPostProcessor後置處理器
      List<BeanDefinitionRegistryPostProcessor> registryProcessors = new LinkedList<BeanDefinitionRegistryPostProcessor>();
      //遍歷手動編碼註冊的BeanFactoryProcessor(檢視入參getBeanFactoryPostProcessors())
      for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
         //如果是BeanDefinitionRegistryPostProcessor
         if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
            BeanDefinitionRegistryPostProcessor registryProcessor =
                  (BeanDefinitionRegistryPostProcessor) postProcessor;
            //先呼叫BeanDefinitionRegistryPostProcessor擴充套件的方法
            registryProcessor.postProcessBeanDefinitionRegistry(registry);
            //放入registryProcessors集合,後續呼叫繼承自父介面的方法
            registryProcessors.add(registryProcessor);
         }
         else {
            //放入regularPostProcessors集合
            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<BeanDefinitionRegistryPostProcessor>();

      // 獲取配置檔案中所有實現了BeanDefinitionRegistryPostProcessor的bean名稱
      String[] postProcessorNames =
            beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
      //遍歷配置檔案中的這些bean
      for (String ppName : postProcessorNames) {
         //如果實現了PriorityOrdered介面
         if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
            //拎出來放入臨時集合中
            currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
            //新增到已執行的set集合中,防止重複執行(後續判斷條件會用到)
            processedBeans.add(ppName);
         }
      }
      //把實現了PriorityOrer介面的這些處理器bean排序
      sortPostProcessors(currentRegistryProcessors, beanFactory);
      //把這些配置檔案中的BeanDefinitionRegistryPostProcessor放入registryProcessors列表中
      registryProcessors.addAll(currentRegistryProcessors);
      //執行BeanDefinitionRegistryPostProcessor中擴充套件的方法
      invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
      //清空臨時集合
      currentRegistryProcessors.clear();

      // 這步特殊處理實現了Ordered介面的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);
      currentRegistryProcessors.clear();

      // 執行剩下的BeanDefinitionRegistryPostProcessors,步驟同上.
      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();
      }

      // 執行所有BeanDrfinitionRegistryPostProcessor(包括手動編碼註冊和配置檔案中配置的)
      // 繼承自BeanFactoryPostProcessor的方法
      invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
      // 執行所有手動編碼註冊的BeanFactoryPostProcessor的後置處理方法
      invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
   }

   else {
      // 直接執行手動編碼註冊的後置處理器的方法
      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);

   // 以下幾步就是對配置檔案中配置的BeanFactory後處理器按實現PriortyOrdered,Ordered以及沒有實現順序介面
   // 分類,分別排序處理,步驟和上面一個樣(這個編輯器不好用,就不多打字了)
   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(priorityOrderedPostProcessors, beanFactory);
   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(orderedPostProcessors, beanFactory);
   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);

   // 由於BeanFactoryPostProcessor後置處理器的方法可能修改了後設資料,例如PropertyPlaceholderConfigurer替換了屬性值的
   // 佔位符,所以需要清理相關快取  
   beanFactory.clearMetadataCache();
}複製程式碼


step6、註冊bean的後置處理器(關於這個處理器,上文中有簡單描述,應該可以幫助大家理解了,不清楚的童鞋先往前翻翻)

/**
 * Instantiate and invoke all registered BeanPostProcessor beans,
 * respecting explicit order if given.
 * <p>Must be called before any instantiation of application beans.
 */
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
   PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}複製程式碼

public static void registerBeanPostProcessors(
      ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
   // 獲取所有配置檔案中配置的BeanPostProcessor
   String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

   //beanProcessorTargetCount = 手動註冊的BeanPostProcessor數量+1+配置檔案中配置的BeanPostProcessor數量   
   //  加1因為額外要註冊BeanPostProcessorChecker記錄日誌。
   int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
   //註冊BeanPostProcessorChecker的作用,是當沒有註冊完後置處理器就開始例項化其他bean,會記錄日誌
   beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

   // BeanPostProcessors按實現了PriorityOrdered,Ordered和未實現順序介面分類,各自排序註冊。(和上文對BeanFactory的後置
   // 處理器的處理方法一個樣).
   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) {
            // 把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.
   sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
   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) {
         // 把MergedBeanDefinitionPostProcessor型別的拎出來
         internalPostProcessors.add(pp);
      }
   }
   sortPostProcessors(orderedPostProcessors, beanFactory);
   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) {
         // 把MergedBeanDefinitionPostProcessor型別的拎出來
         internalPostProcessors.add(pp);
      }
   }
   registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

   // 排序,再次註冊MergedBeanDefinitionPostProcessor型別的bean(註冊過的會被清理再次註冊)
   sortPostProcessors(internalPostProcessors, beanFactory);
   // 從效果上看是讓這些型別的處理器註冊順序排到最後
   registerBeanPostProcessors(beanFactory, internalPostProcessors);

   // 重新註冊ApplicationListenerDetector
   beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}複製程式碼


step7、初始化國際化用到的資源

public static final String MESSAGE_SOURCE_BEAN_NAME = "messageSource";/**
 * Initialize the MessageSource.
 * Use parent's if none defined in this context.
 */
protected void initMessageSource() {
   //獲取BeanFactory
   ConfigurableListableBeanFactory beanFactory = getBeanFactory();
   // 如果存在命名為messageSource的bean (此處為spring硬編碼)
   if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
      // 初始化這個bean
      this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
      // 建立MessageSource和parent MessageSource的聯絡.
      if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
         HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
         if (hms.getParentMessageSource() == null) {
            // Only set parent context as parent MessageSource if no parent MessageSource
            // registered already.
            hms.setParentMessageSource(getInternalParentMessageSource());
         }
      }
      if (logger.isDebugEnabled()) {
         logger.debug("Using MessageSource [" + this.messageSource + "]");
      }
   }
   else {
      // 不存在,這建立一個空的MessageSource去處理獲取資源的請求,並命名為messageSource
      DelegatingMessageSource dms = new DelegatingMessageSource();
      dms.setParentMessageSource(getInternalParentMessageSource());
      this.messageSource = dms;
      beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
      if (logger.isDebugEnabled()) {
         logger.debug("Unable to locate MessageSource with name '" + MESSAGE_SOURCE_BEAN_NAME +
               "': using default [" + this.messageSource + "]");
      }
   }
}複製程式碼


step8、 初始化事件廣播器(觀察者模式)

      不瞭解觀察者模式的童鞋可以先簡單瞭解下,這個模式的實現雖然比較簡單,但是對於瞭解spring的事件廣播和監聽很有幫助。

public static final String APPLICATION_EVENT_MULTICASTER_BEAN_NAME = "applicationEventMulticaster";

/**
 * Initialize the ApplicationEventMulticaster.
 * Uses SimpleApplicationEventMulticaster if none defined in the context.
 * @see org.springframework.context.event.SimpleApplicationEventMulticaster
 */
protected void initApplicationEventMulticaster() {
   // 獲取BeanFactory
   ConfigurableListableBeanFactory beanFactory = getBeanFactory();
   // 包含命名為applicationEventMulticaster的bean(硬編碼的多路廣播器)
   if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
      this.applicationEventMulticaster =
            beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
      if (logger.isDebugEnabled()) {
         logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
      }
   }
   else {
      // 不包含,那麼就建立一個SimpleApplicationEventMulticaster並命名為applicationEventMulticaster
      this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
      beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
      if (logger.isDebugEnabled()) {
         logger.debug("Unable to locate ApplicationEventMulticaster with name '" +
               APPLICATION_EVENT_MULTICASTER_BEAN_NAME +
               "': using default [" + this.applicationEventMulticaster + "]");
      }
   }
}複製程式碼


step9、 初始化其他特殊的bean,有特定的子類容器去實現

/**
 * Template method which can be overridden to add context-specific refresh work.
 * Called on initialization of special beans, before instantiation of singletons.
 * <p>This implementation is empty.
 * @throws BeansException in case of errors
 * @see #refresh()
 */
protected void onRefresh() throws BeansException {
   // For subclasses: do nothing by default.
}複製程式碼


step10、 註冊事件監聽器,釋出需要提前廣播的事件

/**
 * Add beans that implement ApplicationListener as listeners.
 * Doesn't affect other listeners, which can be added without being beans.
 */
protected void registerListeners() {
   // 註冊所有手動編碼新增的事件監聽器
   for (ApplicationListener<?> listener : getApplicationListeners()) {
      getApplicationEventMulticaster().addApplicationListener(listener);
   }

   // 註冊所有配置檔案配置的事件監聽器
   String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
   for (String listenerBeanName : listenerBeanNames) {
      getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
   }

   // 釋出需要提前廣播的事件
   Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
   this.earlyApplicationEvents = null;
   if (earlyEventsToProcess != null) {
      for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
         getApplicationEventMulticaster().multicastEvent(earlyEvent);
      }
   }
}複製程式碼

      下面我們通過具體的事件廣播方法,看下觀察者模式的使用

@Override
public void multicastEvent(ApplicationEvent event) {
   multicastEvent(event, resolveDefaultEventType(event));
}

@Override
public void multicastEvent(final ApplicationEvent event, ResolvableType eventType) {
   ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
   // 遍歷監聽該事件的所有監聽器
   for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
      Executor executor = getTaskExecutor();
      if (executor != null) {
         executor.execute(new Runnable() {
            @Override
            public void run() {
               // 釋出事件
               invokeListener(listener, event);
            }
         });
      }
      else {
         invokeListener(listener, event);
      }
   }
}複製程式碼


step11、 例項化剩下的非延遲載入的單例

String CONVERSION_SERVICE_BEAN_NAME = "conversionService";
/**
 * Finish the initialization of this context's bean factory,
 * initializing all remaining singleton beans.
 */
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
   // 存在型別轉換器,就載入註冊
   if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
         beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
      beanFactory.setConversionService(
            beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
   }

   // 如果沒有預設的embedded value resolver,則註冊一個
   if (!beanFactory.hasEmbeddedValueResolver()) {
      beanFactory.addEmbeddedValueResolver(new StringValueResolver() {
         @Override
         public String resolveStringValue(String strVal) {
            return getEnvironment().resolvePlaceholders(strVal);
         }
      });
   }

   // AOP功能,後續在AOP模組分析
   String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
   for (String weaverAwareName : weaverAwareNames) {
      getBean(weaverAwareName);
   }

   // 停用為了型別匹配建立的類載入器
   beanFactory.setTempClassLoader(null);

   // 凍結修改,快取所有的bean後設資料
   beanFactory.freezeConfiguration();

   // 例項化所有非延遲載入的bean
   beanFactory.preInstantiateSingletons();
}複製程式碼

      下面簡單看下例項化的步驟

@Override
public void preInstantiateSingletons() throws BeansException {
   if (this.logger.isDebugEnabled()) {
      this.logger.debug("Pre-instantiating singletons in " + this);
   }

   // Iterate over a copy to allow for init methods which in turn register new bean definitions.
   // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
   // 獲取所有註冊的bean
   List<String> beanNames = new ArrayList<String>(this.beanDefinitionNames);

   // 遍歷
   for (String beanName : beanNames) {
      RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
      // 非抽象 && 單例 && 非延遲載入
      if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
         // 如果是FactoryBean
         if (isFactoryBean(beanName)) {
            // ‘&’ 字首 + beanName 獲取 FactoryBean 本身
            final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
            boolean isEagerInit;
            // 判斷是否需要立即載入bean
            if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
               isEagerInit = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
                  @Override
                  public Boolean run() {
                     return ((SmartFactoryBean<?>) factory).isEagerInit();
                  }
               }, getAccessControlContext());
            }
            else {
               isEagerInit = (factory instanceof SmartFactoryBean &&
                     ((SmartFactoryBean<?>) factory).isEagerInit());
            }
            if (isEagerInit) {
               getBean(beanName);
            }
         }
         else {
            //普通bean的載入
            getBean(beanName);
         }
      }
   }

   // 實現了SmartInitializingSingleton的單例,可以觸發afterSingletonsInstantiated回撥方法
   for (String beanName : beanNames) {
      Object singletonInstance = getSingleton(beanName);
      if (singletonInstance instanceof SmartInitializingSingleton) {
         final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
         if (System.getSecurityManager() != null) {
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
               @Override
               public Object run() {
                  smartSingleton.afterSingletonsInstantiated();
                  return null;
               }
            }, getAccessControlContext());
         }
         else {
            smartSingleton.afterSingletonsInstantiated();
         }
      }
   }
}複製程式碼


step12、 容器重新整理完成,處理容器啟動後置事件以及釋出完成通知事件

protected void finishRefresh() {
   // 初始化lifecycleProcessor
   initLifecycleProcessor();

   // 獲取所有實現了Lifecycle的bean,並執行start()
   getLifecycleProcessor().onRefresh();

   // 釋出容器啟動完成事件
   publishEvent(new ContextRefreshedEvent(this));

   // 把當前容器註冊到LiveBeansView的集合中
   LiveBeansView.registerApplicationContext(this);
}複製程式碼

      

      好了,spring容器的啟動過程到這裡已經結束了。中間還有很多點可以擴充套件,鑑於本人的水平有限,有些點可能沒有講詳細,後續會盡量再補充一些分析文章,聊勝於無。?


相關文章