action-servlet.xml

ApplicationContext是Spring的核心,Context咱們一般解釋爲上下文環境,我想用「容器」來表述它更容易理解一些,ApplicationContext則是「應用的容器」了:P,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了:) web

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息