深入理解 spring 容器,源碼分析加載過程


Spring框架提供了構建Web應用程序的全功能MVC模塊,叫Spring MVC,通過Spring Core+Spring MVC即可搭建一套穩定的Java Web項目。本文通過Spring MVC源碼分析介紹它的核心實現原理。

    Tomcat服務器啟動入口文件是web.xml,通過在其中配置相關的Listener和Servlet即可加載Spring MVC所需數據。基於Spring MVC最簡單的配置如下。

<!-- 加載Spring配置文件 -->  
<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>  
    classpath:spring-context*.xml  
    </param-value>  
</context-param>  
<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>  
  
<!-- 加載spring mvc -->  
<servlet>  
    <servlet-name>spring3mvc</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>  
        classpath:spring-mvc*.xml  
        </param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>  
</servlet>  
  
<servlet-mapping>  
    <servlet-name>spring3mvc</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping>  
  • 創建容器

        ContextLoaderListener基於Web上下文級別的監聽器在啟動服務器時就創建ApplicationContext並且將配置的Spring Bean加載到容器中中。

        DispatcherServlet是一個請求分發控制器,所有匹配的URL都會通過該Servlet分發執行,在創建Servlet對象時會初始化Spring MVC相關配置。

        在web.xml中,我們看到基於ContextLoaderListener和DispatcherServlet都可以配置spring相關的XML,值得說明的是這兩種方式加載spring的ApplicationContext上下文對象不是合並存儲的,具體可參考http://blog.csdn.net/madun/article/details/8988860。所以個人建議,基於mvc相關的spring配置由DispatcherServlet加載,而其余的JavaBean都交給ContextLoaderListener加載

一、ContextLoaderListener

        ContextLoaderListener是一個實現了ServletContextListener接口的監聽器,在啟動項目時會觸發contextInitialized方法(該方法主要完成ApplicationContext對象的創建),在關閉項目時會觸發contextDestroyed方法(該方法會執行ApplicationContext清理操作)。

public class ContextLoaderListener extends ContextLoader implements ServletContextListener

        ConextLoaderListener加載Spring上下文的過程可以用下圖表示,黃色區塊是核心代碼。


簡單介紹一下上圖的運行流程:

①啟動項目時觸發contextInitialized方法,該方法就做一件事:通過父類contextLoader的initWebApplicationContext方法創建Spring上下文對象。

②initWebApplicationContext方法做了三件事:創建WebApplicationContext;加載對應的Spring文件創建里面的Bean實例;將WebApplicationContext放入ServletContext(就是Java Web的全局變量)中。

③createWebApplicationContext創建上下文對象,支持用戶自定義的上下文對象,但必須繼承自ConfigurableWebApplicationContext,而Spring MVC默認使用XmlWebApplicationContext作為ApplicationContext(它僅僅是一個接口)的實現。

④configureAndRefreshWebApplicationContext方法用於封裝ApplicationContext數據並且初始化所有相關Bean對象。它會從web.xml中讀取名為contextConfigLocation的配置,這就是spring xml數據源設置,然后放到ApplicationContext中,最后調用傳說中的refresh方法執行所有Java對象的創建。

⑤完成ApplicationContext創建之后就是將其放入ServletContext中,注意它存儲的key值常量。

//常量  
public static final String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";  

servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);  

注:要獲取 ContextLoader級別的IOC容器對象可以這樣寫:

WebApplicationContext rootContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 

 二、DispatcherServlet

DispatcherServlet是前端控制器設計模式的實現,提供Spring Web MVC的集中訪問點,而且負責職責的分派,而且與Spring IoC容器無縫集成,從而可以獲得Spring的所有好處。

要了解DispatcherServlet是如何加載容器,需要先了解它的繼承關系,如下圖所示:


 

如果在web.xml中設置了Servlet的<load-on-startup>1</load-on-startup>,則表示隨項目啟動,而我們知道Servelt創建時會首先調用init方法,所以繼承了HttpServlet的HttpServletBean就是關鍵入口了。那么整個代碼運行流程如下圖所示。

 

①HttpServletBean.init方法中執行initServletBean方法進行初始化操作,當然該方法在HttpServletBean是空方法,所以需要子類重寫。

②FrameworkServlet.initServletBean子類不負眾望,重寫了initServletBean方法,該方法最核心的操作就是調用initWebApplicationContext()執行上下文Bean初始化。

③FrameworkServlet.initWebApplicationContext方法首先獲取自己的雙親上下文(也就是ContextLoaderListener初始化成功的WebApplicationContext);然后創建或者獲取當前Servelet的WebApplicationContext。

④無論是自己創建還是獲取現有的WebApplicationContext,最終都會讓Servelt級別的WebApplicationContext執行configureAndRefreshWebApplicationContext()方法進行上下文容器初始化。

 

通過以上幾步即可創建一個完整的IOC容器,而完成容器創建之后,DispatcherServlet還做了一件事:初始化Servelt控制器必備對象,這個是在initWebApplicationContext()方法中通過調用onRefresh(wac)方法實現的。而onRefresh也被重寫過,如果要了解怎么初始化Servlet控制器必備對象可以查看DispatcherServlet的onRefresh方法了解。

/** 
 * This implementation calls {@link #initStrategies}. 
 */  
@Override  
protected void onRefresh(ApplicationContext context) {  
    initStrategies(context);  
}  
  
/** 
 * Initialize the strategy objects that this servlet uses. 
 * <p>May be overridden in subclasses in order to initialize further strategy objects. 
 */  
protected void initStrategies(ApplicationContext context) {  
    initMultipartResolver(context);  
    initLocaleResolver(context);  
    initThemeResolver(context);  
    initHandlerMappings(context);  
    initHandlerAdapters(context);  
    initHandlerExceptionResolvers(context);  
    initRequestToViewNameTranslator(context);  
    initViewResolvers(context);  
    initFlashMapManager(context);  
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM