spring原始碼學習之:spring容器的applicationContext啟動過程

Love Lenka發表於2016-09-18

 Spring 容器像一臺構造精妙的機器,我們透過配置檔案向機器傳達控制資訊,機器就能夠按照設定的模式進行工作。如果我們將Spring容器比喻為一輛汽車,可以將 BeanFactory看成汽車的發動機,而ApplicationContext則是 整輛汽車,它不但包括髮動機,還包括離合器、變速器以及底盤、車身、電氣裝置等其他元件。在ApplicationContext內,各個元件按部就班、 有條不紊地完成汽車的各項功能。

ApplicationContext內部封裝 了一個BeanFactory物件,來實現對容器的操作,初始化完成之後,BeanFactory封裝了bean的資訊,而 ApplicationContext透過訪問這個物件獲取bean的物件資訊(BeanDefinition/Bean物件,都是由 BeanFactory實際建立並管理的),為了實現介面的統一,ApplicationContext也實現了一系列的BeanFactory介面(可 以說ApplicationContext對BeanFactory物件實現一種代理)。ApplicationContext建立在 BeanFactory的基礎之上,對配置物件的管理最終還是交於一個DefaultListableBeanFactory來完成(裝配地址/訪問 等),而ApplicationContext在應用這個DefaultListableBeanFactory物件的基礎上,不 僅實現了BeanFactory介面提供的功能方法,並且黏合了一些面向應用的功能,如資源/國際化支援/框架事件支援等,並且將一些原先需要手動設定到 BeanFactory的屬性透過配置檔案中配置的形式代替(如工廠後處理器 BeanPostProcessor/InstantiationAwareBeanPostProcessor)

同樣,因為對於BeanDefinition和 bean物件的管理是由上下文持有的beanfactory物件完成的,使用者不需要擁有這樣的介面,因此,ApplicationContext的介面體 系中並沒有BeanDefinitionRegistry,SingletonBeanRegistry以及 AutowireCapableBeanFactory介面(ApplicationContext可以訪問一些介面方法在上述介面中也定義,但這些方法 提供者為BeanFactory體系中的其他介面,BeanFactory介面體系中的介面之間有重複定義方法的)。

 

內部工作機制(Spring容器ApplicationContext的初始化                                                                                    

(一) 首先來看建立ApplicationContext ,以ClassPathXmlApplicationContext為例:

ApplicationContext = new ClassPathXmlApplicationContext(xmlPath);  

原始碼如下:

public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext {

public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
                  this(new String[] {configLocation});
       }

public ClassPathXmlApplicationContext(String[] configLocations) throws BeansException {
                  this(configLocations, (ApplicationContext) null);
       }

//。。。。。。省略幾個過載的建構函式

public ClassPathXmlApplicationContext(String[] configLocations, ApplicationContext parent)
                                                                                                                         throws BeansException {

          super(parent);
                 this.configLocations = configLocations;

          //IoC容器的初始化過程,其初始化過程的大致步驟由AbstractApplicationContext來定義   
                 refresh();
       }

關鍵之處在於refresh方法,此方法繼承於ClassPathXmlApplicationContext的間接父類:

public abstract class AbstractApplicationContext extends DefaultResourceLoader
                                                        implements ConfigurableApplicationContext, DisposableBean
{

Spring的AbstractApplicationContext是 ApplicationContext抽象實現類,該抽象類的refresh()方法定義了Spring容器在載入配置檔案後的各項處理過程,這些處理過 程清晰刻畫了Spring容器啟動時所執行的各項操作(建立Spring容器如ClassPathXmlApplicationContext)。下面, 我們來看一下refresh()內部定義了哪些執行邏輯:

public void refresh() throws BeansException, IllegalStateException {
              synchronized (this.startupShutdownMonitor) {
                    // Prepare this context for refreshing.
                    prepareRefresh();

             // Tell the subclass to refresh the internal bean factory       

            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();--------(1)

             // Prepare the bean factory for use in this context.
                    prepareBeanFactory(beanFactory);

             try {
                          // Allows post-processing of the bean factory in context subclasses.
                          postProcessBeanFactory(beanFactory);

                   // Invoke factory processors registered as beans in the context.
                          invokeBeanFactoryPostProcessors(beanFactory);-------------------------------------(2)

                   // Register bean processors that intercept bean creation
                          registerBeanPostProcessors(beanFactory);---------------------------------------------(3)

                   // Initialize message source for this context.
                          initMessageSource();-------------------------------------------------------------------------(4)

                   // Initialize event multicaster for this context.
                          initApplicationEventMulticaster();-----------------------------------------------------------(5)

                   // Initialize other special beans in specific context subclasses.
                          onRefresh();------------------------------------------------------------------------------------(6)

                   // Check for listener beans and register them.
                          registerListeners();----------------------------------------------------------------------------(7)

                   // Instantiate singletons this late to allow them to access the message source.
                          beanFactory.preInstantiateSingletons();--------------------------------------------------(8)

                   // Last step: publish corresponding event.
                          publishEvent(new ContextRefreshedEvent(this));---------------------------------------(9)
                    } catch (BeansException ex) {
                          // Destroy already created singletons to avoid dangling resources.
                          beanFactory.destroySingletons();
                          throw ex;
                   }
             }
       }

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
             refreshBeanFactory();
             ConfigurableListableBeanFactory beanFactory = getBeanFactory();

      return beanFactory;
       } 

protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;

public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;

 

1.初始化BeanFactory:根據配置檔案例項化 BeanFactory,getBeanFactory()方法由具體子類實現。在這一步裡,Spring將配置檔案的資訊解析成為一個個的 BeanDefinition物件並裝入到容器的Bean定義登錄檔(BeanDefinitionRegistry)中,但此時Bean還未初始 化;obtainFreshBeanFactory()會呼叫自身的refreshBeanFactory(),而 refreshBeanFactory()方法由子類AbstractRefreshableApplicationContext實現,該方法返回了一個建立的DefaultListableBeanFactory物件,這個物件就是由ApplicationContext管理的BeanFactory容器物件

這一步的操作相當於,如果我們在自己的應用程式碼中不用ApplicationContext而直接用BeanFactory時建立BeanFactory物件的操作

核心程式碼如下:

public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext { }

 /** 該ApplicationContext管理的BeanFactory容器物件*/
       private DefaultListableBeanFactory beanFactory;

protected final void refreshBeanFactory() throws BeansException {
              // Shut down previous bean factory, if any.
              ConfigurableListableBeanFactory oldBeanFactory = null;
              synchronized (this.beanFactoryMonitor) {
                      oldBeanFactory = this.beanFactory;
              }
              if (oldBeanFactory != null) {
                      oldBeanFactory.destroySingletons();
                      synchronized (this.beanFactoryMonitor) {
                             this.beanFactory = null;
                      }
              }

       // Initialize fresh bean factory.
              try {

               // 建立容器物件
                      DefaultListableBeanFactory beanFactory = createBeanFactory();

               // Customize the internal bean factory used by this context
                      customizeBeanFactory(beanFactory);

               // 裝載配置檔案,並傳入相關聯的BeanFactory物件,作為BeanDefinition的容器
                      loadBeanDefinitions(beanFactory);
                      synchronized (this.beanFactoryMonitor) {
                            this.beanFactory = beanFactory;
                      }
              }  catch (IOException ex) {
                      throw new ApplicationContextException(
                                  "I/O error parsing XML document for application context [" + getDisplayName() + "]", ex);
              }
       }

// 建立Spring預設的容器物件

protected DefaultListableBeanFactory createBeanFactory() {
              return new DefaultListableBeanFactory(getInternalParentBeanFactory());
       }

// 該方法為一個鉤子方法,子類可以覆蓋它對當前上下文管理的BeanFactory提供客戶化操作,也可以忽略

protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
       }

// 裝載配置檔案的方法,需要子類實現

protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory)
                                                                                                     throws IOException, BeansException;

 

對於上面裝載配置檔案的方法,由其子類擴充套件實現:

public abstract class AbstractXmlApplicationContext extends AbstractRefreshableApplicationContext  {}

protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException {
             // 使用XMLBeanDefinitionReader來載入bean定義資訊的XML檔案,傳入關聯的BeanFactory

      XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

      // 這裡配置reader的環境,其中ResourceLoader是我們用來定位bean定義資訊資源位置的
             // 因為上下文字身實現了ResourceLoader介面,所以可以直接把上下文作為ResourceLoader傳遞入

      beanDefinitionReader.setResourceLoader(this);
             beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

      // Allow a subclass to provide custom initialization of the reader,
             // then proceed with actually loading the bean definitions.
             initBeanDefinitionReader(beanDefinitionReader);

      // 這裡轉到定義好的XmlBeanDefinitionReader中對載入bean資訊進行處理 
             loadBeanDefinitions(beanDefinitionReader);
       } 

protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
             Resource[] configResources = getConfigResources();
             if (configResources != null) {
                     reader.loadBeanDefinitions(configResources);
             }
             String[] configLocations = getConfigLocations();
             if (configLocations != null) {
                     reader.loadBeanDefinitions(configLocations);
             }
       }

reader.loadBeanDefinitions(configLocations);涉及到XmlBeanDefinitionReader 工具類的使用(以後整理

2.呼叫工廠後處理器:根據反射機制從BeanDefinitionRegistry中找出所有BeanFactoryPostProcessor型別的Bean,並呼叫其postProcessBeanFactory()介面方法;

經過第一步載入配置檔案,已經把配置檔案中定義的所有bean裝載到 BeanDefinitionRegistry這個Beanfactory中,對於ApplicationContext應用來說這個 BeanDefinitionRegistry型別的BeanFactory就是Spring預設的 DefaultListableBeanFactory

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
                                                           implements ConfigurableListableBeanFactory, BeanDefinitionRegistry

在這些被裝載的bean中,若有型別為BeanFactoryPostProcessor的bean(配置檔案中配置的),則將對應的BeanDefinition生成BeanFactoryPostProcessor物件

容器掃描BeanDefinitionRegistry中的BeanDefinition,使 用java反射自動識別出Bean工廠後處理器(實現BeanFactoryPostProcessor介面)的bean,然後呼叫這些bean工廠後處 理器對BeanDefinitionRegistry中的BeanDefinition進行加工處理,可以完成以下兩項工作(當然也可以有其他的操作,用 戶自己定義):

1 對使用到佔位符的<bean>元素標籤進行解析,得到最終的配置值,這意味著對一些半成品式的BeanDefinition物件進行加工處理 並取得成品的BeanDefinition物件。2 對BeanDefinitionRegistry中的BeanDefinition進行掃描,透過Java反射機制找出所有屬性編輯器的Bean(實現 java.beans.PropertyEditor介面的Bean),並自動將它們註冊到Spring容器的屬性編輯器登錄檔中 (PropertyEditorRegistry),這個Spring提供了實現:CustomEditorConfigurer,它實現了 BeanFactoryPostProcessor,用它來在此註冊自定義屬性編輯器;

AbstractApplicationContext中的程式碼如下:

protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
             // Invoke factory processors registered with the context instance.
             for (Iterator it = getBeanFactoryPostProcessors().iterator(); it.hasNext();) {
                   BeanFactoryPostProcessor factoryProcessor = (BeanFactoryPostProcessor) it.next();
                   factoryProcessor.postProcessBeanFactory(beanFactory);
             }

      // Do not initialize FactoryBeans here: We need to leave all regular beans
             // 透過ApplicatinContext管理的beanfactory獲取已經註冊的BeanFactoryPostProcessor型別的bean的名字

             String[] factoryProcessorNames =
                              beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

      // Separate between BeanFactoryPostProcessors that implement the Ordered
             // interface and those that do not.
             List orderedFactoryProcessors = new ArrayList();
             List nonOrderedFactoryProcessorNames = new ArrayList();
             for (int i = 0; i < factoryProcessorNames.length; i++) {
                   if (isTypeMatch(factoryProcessorNames[i], Ordered.class)) {

                   // 呼叫beanfactory的getBean取得所有的BeanFactoryPostProcessor物件
                         orderedFactoryProcessors.add(beanFactory.getBean(factoryProcessorNames[i]));
                   }
                   else {
                         nonOrderedFactoryProcessorNames.add(factoryProcessorNames[i]);
                   }
             }

      // First, invoke the BeanFactoryPostProcessors that implement Ordered.
             Collections.sort(orderedFactoryProcessors, new OrderComparator());
             for (Iterator it = orderedFactoryProcessors.iterator(); it.hasNext();) {
                  BeanFactoryPostProcessor factoryProcessor = (BeanFactoryPostProcessor) it.next();

           // 執行BeanFactoryPostProcessor的方法,傳入當前持有的beanfactory物件,以獲取要操作的 

           // BeanDefinition
                  factoryProcessor.postProcessBeanFactory(beanFactory);
             }
             // Second, invoke all other BeanFactoryPostProcessors, one by one.
             for (Iterator it = nonOrderedFactoryProcessorNames.iterator(); it.hasNext();) {
                  String factoryProcessorName = (String) it.next();
                  ((BeanFactoryPostProcessor) getBean(factoryProcessorName)).

                                                                      postProcessBeanFactory(beanFactory);
             }
       }

 

BeanFactoryPostProcessor介面程式碼如下,實際的操作由使用者擴充套件並配置(擴充套件點,如何擴充套件?

package org.springframework.beans.factory.config;

public interface BeanFactoryPostProcessor {

     /**
             * Modify the application context's internal bean factory after its standard
            */
             void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;

}

 

3.註冊Bean後處理器:根據反射機制從BeanDefinitionRegistry中找出所有BeanPostProcessor型別的Bean,並將它們註冊到容器Bean後處理器的登錄檔中;

AbstractApplicatinContext中對應程式碼如下:

protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
              String[] processorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

         // Register BeanPostProcessorChecker that logs an info message when
                int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 +  

                                                                                                                        processorNames.length;
                beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory,  

                                                                                                                beanProcessorTargetCount));
                List orderedProcessors = new ArrayList();
                List nonOrderedProcessorNames = new ArrayList();  

         for (int i = 0; i < processorNames.length; i++) {
                      if (isTypeMatch(processorNames[i], Ordered.class)) {
                           orderedProcessors.add(getBean(processorNames[i]));
                      }
                      else {
                           nonOrderedProcessorNames.add(processorNames[i]);
                      } 
                }

         // First, register the BeanPostProcessors that implement Ordered.
                Collections.sort(orderedProcessors, new OrderComparator());
                for (Iterator it = orderedProcessors.iterator(); it.hasNext();) {

               // 註冊bean後處理器,該方法定義於ConfigurableBeanFactory介面
                      beanFactory.addBeanPostProcessor((BeanPostProcessor) it.next());
                }
                // Second, register all other BeanPostProcessors, one by one.
                for (Iterator it = nonOrderedProcessorNames.iterator(); it.hasNext();) {
                      String processorName = (String) it.next();
                      beanFactory.addBeanPostProcessor((BeanPostProcessor) getBean(processorName));
                }
       }

整段程式碼類似於第三步的呼叫工廠後處理器,區別之處在於,工廠後處理器在獲取後立即呼叫,而 Bean後處理器在獲取後註冊到上下文持有的beanfactory中,供以後操作呼叫(在使用者獲取bean的過程中,對已經完成屬性設定工作的Bean 進行後續加工,他加工的是bean,而工廠後處理器加工的是BeanDefinition)

BeanPostProcessor 介面程式碼如下,實際的操作由使用者擴充套件並配置(擴充套件點,如何擴充套件?

package org.springframework.beans.factory.config;

public interface BeanPostProcessor {
             Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
             Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;

}

 

4.初始化訊息源:初始化容器的國際化資訊資源;

原始碼如下:

protected void initMessageSource() {
                // 補充
       }

 

5.初始化應用上下文事件廣播器;(觀察者模式中的具體主題角色,持有觀察者角色的集合,稱為登錄檔)

AbstractApplciationContext擁有一個 applicationEventMulticaster 成員變數,applicationEventMulticaster 提供了容器監聽器的登錄檔,成其為事件廣播器。在第七步中將會將事件監聽器裝入其中

AbstractApplicationContext中的程式碼如下:

private ApplicationEventMulticaster applicationEventMulticaster;

protected void initApplicationEventMulticaster() {

        // "applicationEventMulticaster",先看配置檔案中有無配置該型別類(使用者擴充套件 擴充套件點,如何擴充套件
               if (containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
                    this.applicationEventMulticaster = (ApplicationEventMulticaster)
                    getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
               }
               else {

              // 若沒有,則應用Spring框架提供的事件廣播器例項
                     this.applicationEventMulticaster = new SimpleApplicationEventMulticaster();
               }
       }
       public boolean containsLocalBean(String name) {
               return getBeanFactory().containsLocalBean(name);
       }

public Object getBean(String name, Class requiredType) throws BeansException {
              return getBeanFactory().getBean(name, requiredType);
       }

Spring初始化事件廣播器,使用者可以在配置檔案中為容器定義一個自定義的事件廣播器 (bean的名稱要為"applicationEventMulticaster"),只要實現ApplicationEventMulticaster 就可以了,Spring在此會根據beanfactory自動獲取。如果沒有找到外部配置的事件廣播器,Spring使用 SimpleApplicationEventMulticaster作為事件廣播器。

 

6.初始化其他特殊的Bean:這是一個鉤子方法,子類可以藉助這個鉤子方法執行一些特殊的操作:如AbstractRefreshableWebApplicationContext就使用該鉤子方法執行初始化ThemeSource的操作;

protected void onRefresh() throws BeansException {(擴充套件點,如何擴充套件

package org.springframework.context;
            // For subclasses: do nothing by default.
       }

 

7.註冊事件監聽器;(觀察者模式中的觀察者角色)

Spring根據上下文持有的beanfactory物件,從它的 BeanDefinitionRegistry中找出所有實現 org.springfamework.context.ApplicationListener的bean,將BeanDefinition物件生成 bean,註冊為容器的事件監聽器,實際的操作就是將其新增到事件廣播器所提供的監聽器登錄檔中

AbstractApplicationContext中的程式碼如下:

/** Statically specified listeners */

private List applicationListeners = new ArrayList();

public List getApplicationListeners() {
              return this.applicationListeners;
       }

protected void registerListeners() {
              // Register statically specified listeners first.
              for (Iterator it = getApplicationListeners().iterator(); it.hasNext();) {
                     addListener((ApplicationListener) it.next());
              }
              // 獲取ApplicationListener型別的所有bean,即事件監聽器
              // uninitialized to let post-processors apply to them!
              Collection listenerBeans = getBeansOfType(ApplicationListener.class, true, false).values();
              for (Iterator it = listenerBeans.iterator(); it.hasNext();) {

              // 將事件監聽器裝入第五步初始化的事件廣播器
                     addListener((ApplicationListener) it.next());
              }
       }

public Map getBeansOfType(Class type, boolean includePrototypes, boolean allowEagerInit)
                                                                                                                          throws BeansException {

         return getBeanFactory().getBeansOfType(type, includePrototypes, allowEagerInit);
       }

protected void addListener(ApplicationListener listener) {
                getApplicationEventMulticaster().addApplicationListener(listener);
       }

ApplicationListener 的原始碼如下:(擴充套件點,如何擴充套件

package org.springframework.context;

import java.util.EventListener;

/**
        * Interface to be implemented by application event listeners.
        * @see org.springframework.context.event.ApplicationEventMulticaster
       */
       public interface ApplicationListener extends EventListener {
                void onApplicationEvent(ApplicationEvent event);

}

 

8.初始化singleton的Bean:例項化所有singleton的Bean,並將它們 放入Spring容器的快取中;這就是和直接在應用中使用BeanFactory的區別之處,在建立ApplicationContext物件時,不僅創 建了一個BeanFactory物件,並且還應用它例項化所有單例項的bean。

AbstractApplicationContext中的程式碼如下:

beanFactory.preInstantiateSingletons();

關於BeanFactory體系的程式碼參照。。。。。。

 

9.釋出上下文重新整理事件:在此處時容器已經啟動完成,釋出容器refresh事件(ContextRefreshedEvent)

建立上下文重新整理事件,事件廣播器負責將些事件廣播到每個註冊的事件監聽器中。

publishEvent(new ContextRefreshedEvent(this));

public void publishEvent(ApplicationEvent event) {
              Assert.notNull(event, "Event must not be null");

       // 在此獲取事件廣播器,並呼叫其方法釋出事件:呼叫所有註冊的監聽器的方法
              getApplicationEventMulticaster().multicastEvent(event);
              if (this.parent != null) {
                    this.parent.publishEvent(event);
              }
       }

 

至此,ApplicationContext物件就完成了初始化工作:建立 BeanFactory來裝配BeanDefiniton,加工處理BeanDefiniton,註冊了bean後處理器,初始化了訊息資源,初始化了應 用上下文事件廣播器,註冊了事件監聽器,初始化了所有singleton的bean,最後釋出上下文重新整理事件

相關文章