【Spring】中ApplicationContext加載機制

Spring中ApplicationContext加載機制。 
          加載器目前有兩種選擇:ContextLoaderListener和ContextLoaderServlet。 
         這兩者在功能上完全等同,只是一個是基於Servlet2.3版本中新引入的Listener接口實現,而另一個基於Servlet接口實現。開發中可根據目標Web容器的實際情況進行選擇。

配置非常簡單,在web.xml中增加: 
<listener> 
  <listener-class> 
       org.springframework.web.context.ContextLoaderListener 
  </listener-class> 
</listener> 
或: 
<servlet> 
    <servlet-name>context</servlet-name> 
    <servlet-class> 
       org.springframework.web.context.ContextLoaderServlet 
    </servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
  
          通過以上配置,Web容器會自動加載/WEB-INF/applicationContext.xml初始化 
ApplicationContext實例,如果需要指定配置文件位置,可通過context-param加以指定: 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/myApplicationContext.xml</param-value> 
</context-param>

        配置完成之後,即可通過 
 WebApplicationContextUtils.getWebApplicationContext方法在Web應用中獲取ApplicationContext引用。

如:ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(); 
    LoginAction action=(LoginAction)ctx.getBean("action");

 

=============================

 

Spring中WebApplicationContext的研究 

ApplicationContext是Spring的核心,Context我們通常解釋爲上下文環境,我想用“容器”來表述它更容易理解一些,ApplicationContext則是“應用的容器”,Spring把Bean放在這個容器中,在需要的時候,用getBean方法取出,雖然我沒有看過這一部分的源代碼,但我想它應該是一個類似Map的結構。 
在Web應用中,我們會用到WebApplicationContext,WebApplicationContext繼承自ApplicationContext,先讓我們看看在Web應用中,怎麼初始化WebApplicationContext,在web.xml中定義: 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/applicationContext.xml</param-value> 
</context-param> 

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<!-- OR USE THE CONTEXTLOADERSERVLET INSTEAD OF THE LISTENER 
<servlet> 
    <servlet-name>context</servlet-name> 
    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> 
    <!-- 自動啓動 -->

<load-on-startup>1</load-on-startup> 
</servlet> 
--> 

可以看出,有兩種方法,一個是用ContextLoaderListener這個Listerner,另一個是ContextLoaderServlet這個Servlet,這兩個方法都是在web應用啓動的時候來初始化WebApplicationContext,我個人認爲Listerner要比Servlet更好一些,因爲Listerner監聽應用的啓動和結束,而Servlet得啓動要稍微延遲一些,如果在這時要做一些業務的操作,啓動的前後順序是有影響的。 

那麼在ContextLoaderListener和ContextLoaderServlet中到底做了什麼呢? 
以ContextLoaderListener爲例,我們可以看到 
public void contextInitialized(ServletContextEvent event) { 
  this.contextLoader = createContextLoader(); 
  this.contextLoader.initWebApplicationContext(event.getServletContext()); 

protected ContextLoader createContextLoader() { 
  return new ContextLoader(); 

ContextLoader是一個工具類,用來初始化WebApplicationContext,其主要方法就是initWebApplicationContext,我們繼續追蹤initWebApplicationContext這個方法(具體代碼我不貼出,大家可以看Spring中的源碼),我們發現,原來ContextLoader是把WebApplicationContext(XmlWebApplicationContext是默認實現類)放在了ServletContext中ServletContext也是一個“容器”,也是一個類似Map的結構,而WebApplicationContext在ServletContext中的KEY就是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,我們如果要使用WebApplicationContext則需要從ServletContext取出,Spring提供了一個WebApplicationContextUtils類,可以方便的取出WebApplicationContext,只要把ServletContext傳入就可以了。 

上面我們介紹了WebApplicationContext在Servlet容器中初始化的原理,一般的Web應用就可以輕鬆的使用了,但是,隨着Struts的廣泛應用,把Struts和Spring整個起來,是一個需要面對的問題,Spring本身也提供了Struts的相關類,主要使用的有org.springframework.web.struts.ActionSupport,我們只要把自己的Action繼承自ActionSupport,就是可以調用ActionSupport中getWebApplicationContext()的方法取出WebApplicationContext,但這樣一來在Action中,需要取得業務邏輯的地方都要getBean,看上去不夠簡潔,

所以Spring又提供了另一個方法用org.springframework.web.struts.ContextLoaderPlugIn,這是一個Struts的Plug,在Struts啓動時加載,對於Action,可以像管理Bean一樣來管理,在struts-config.xml中Action的配置變成類似下面的樣子 
<action attribute="aForm" name="aForm" path="/aAction" scope="request"  type="org.springframework.web.struts.DelegatingActionProxy"> 
  <forward name="forward" path="forward.jsp" /> 
</action> 
注意type變成了org.springframework.web.struts.DelegatingActionProxy,之後我們需要建立action-servlet.xml這樣的文件,action-servlet.xml符合Spring的spring-beans.dtd標準,在裏面定義類似下面的 
<bean name="/aAction" class="com.web.action.Aaction" singleton="false"> 
  <property name="businessService"> 
    <ref bean="businessService"/> 
  </property> 
</bean> 

com.web.action.Aaction是Action的實現類,businessService是需要的業務邏輯,Spring會把businessService注入到Action中,在Action中只要寫businessService的get和set方法就可以了,還有一點,action的bean是singleton="false",即每次新建一個實例,這也解決了Struts中Action的線程同步問題,具體過程是當用戶做“/aAction”的HTTP請求(當然應該是“/aAction.do”),Struts會找到這個Action的對應類org.springframework.web.struts.DelegatingActionProxy,DelegatingActionProxy是個代理類,它會去找action-servlet.xml文件中“/aAction”對應的真正實現類,然後把它實例化,同時把需要的業務對象注入,然後執行Action的execute方法。 

使用了ContextLoaderPlugIn,在struts-config.xml中變成類似這樣配置 
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> 
  <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml" /> 
</plug-in> 
而在web.xml中不再需要ContextLoaderListener或是ContextLoaderServlet。 

說到這裏不知道大家會不會有這樣的問題,如果使用ContextLoaderPlugIn,如果我們有些程序是脫離Struts的Action環境,我們怎麼處理,比如我們要自定義標記庫,在標記庫中,我們需要調用Spring管理的業務層邏輯對象,這時候我們就很麻煩,因爲只有在action中動態注入業務邏輯,其他我們似乎不能取得Spring的WebApplicationContext。 

別急,我們還是來看一下ContextLoaderPlugIn的源碼(源碼不再貼出),我們可以發現,原來ContextLoaderPlugIn仍然是把WebApplicationContext放在ServletContext中,只是這個KEY不太一樣了,這個KEY值爲ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX+ModuleConfig.getPrefix()(具體請查看源代碼),這下好了,我們知道了WebApplicationContext放在哪裏,只要我們在Web應用中能夠取到ServletContext也就能取到WebApplicationContext了:)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章