如果需要使用第三方MVC框架,則不能在web.xml文件中配置ApplicationContext的啟動。但是,ApplicationContext是Spring的容器,負(fù)責(zé)管理所有的組件,從業(yè)務(wù)邏輯層組件,到持久層組件,都必須運行在Spring容器中。因此,必須在Web應(yīng)用啟動時,創(chuàng)建Spring的ApplicationContext實例。事實上,Spring ApplictionContext作為IoC容器,總應(yīng)該優(yōu)先加載。
不管采用怎樣的方法,Spring容器都應(yīng)該在應(yīng)用啟動時,自動加載。為了讓Spring容器能自動加載,通常有兩個做法:
1.讓MVC框架負(fù)責(zé)創(chuàng)建ApplicationContext實例,MVC框架加載時自動創(chuàng)建Spring容器。Struts就是采用這種機制與Spring整合。
2.采用load-on-startup Servlet實現(xiàn)。
根據(jù)Servlet2.3標(biāo)準(zhǔn),所有的ServletContextListener都會比Servlet優(yōu)先加載——即使是load-on-startup Servlet。ApplicationContext實例是Spring容器,負(fù)責(zé)管理應(yīng)用中所有的組件,包括業(yè)務(wù)邏輯層組件和持久層組件。因此,應(yīng)該盡可能早的創(chuàng)建Spring容器。
為此,應(yīng)該優(yōu)先采用listener創(chuàng)建ApplicationContext。只是,ServletContextListener是從Servlet 2.3才出現(xiàn)的規(guī)范。如果使用了不支持Servlet2.3以上的Web服務(wù)器,則只能放棄ServletContextListener,采用load-on-startup Servlet策略。
Spring管理的組件相當(dāng)多,如果將所有的組件部署在同一個配置文件里。不僅會降低配置文件的可讀性,增大修改配置文件時引入錯誤的可能性,也不符合軟件工程“分而治之”的規(guī)則。通常推薦服務(wù)層對象,業(yè)務(wù)邏輯對象,DAO對象都存在于互不相同的Context中,而表現(xiàn)層對象如Spring MVC控制器,則被配置在表現(xiàn)層Context中。甚至將某個特定模塊的組件部署在單獨的Context中。
實際的應(yīng)用中,Spring的配置文件通常不只一個,而是按功能被分成多個。好在,所有負(fù)責(zé)加載Spring容器的工具都可同時加載多個配置文件。
一. 采用ContextLoaderListener創(chuàng)建ApplicationContext
使用ContextLoaderListener創(chuàng)建ApplicationContext必須服務(wù)器支持listener,下面這些服務(wù)器都是支持Listener的,如果使用這些服務(wù)器,則可以使用ContextLoaderListener創(chuàng)建ApplicationContext實例:
1.Apache Tomcat 4.x+ 。
2.etty 4.x+ 。
3.Resin 2.1.8+ 。
4.Orion 2.0.2+ 。
5.BEA WebLogic 8.1 SP3。
Spring提供ServletContextListener的一個實現(xiàn)類ContextLoaderListener,該類可以作為listener使用,它會在創(chuàng)建時候自動查找WEB-INF/下的applicationContext.xml文件,因此,如果只有一個配置文件,并且文件名為applicationContext.xml,只需在web.xml文件中增加如下一段即可:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果有多個配置文件需要載入,則考慮使用<context-param>元素來確定配置文件的文件名。ContextLoaderListener加載時,會查找名為contextConfigLocation的參數(shù)。因此,配置context-param時參數(shù)名字應(yīng)該是contextConfigLocation。
帶多個配置文件的web.xml文件如下:
<!-- XML文件的文件頭-->
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- web.xml文件的DTD等信息-->
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!-- 確定多個配置文件-->
在web.xml文件中加載Spring容器,這是最常見的做法。Spring自己的MVC框架就是采用這種策略。
關(guān)于讓MVC框架負(fù)責(zé)創(chuàng)建ApplicationContext實例的情況比較多,因為每個MVC框架的啟動機制有區(qū)別,因此加載ApplicationContext的方式也各有不同。
對于在web.xml配置文件中配置ApplicationContext的自動創(chuàng)建有兩種策略:
1.利用ServletContextListener實現(xiàn)。
<context-param>
<!-- 參數(shù)名為contextConfigLocation -->
<param-name>contextConfigLocation</param-name>
<!-- 多個配置文件之間以,隔開 -->
<param-value>/WEB-INF/daoContext.xml,/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 采用listener創(chuàng)建ApplicationContext實例-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
如果沒有contextConfigLocation制定配置文件,Spring自動查找applicationContext.xml配置文件。
如果有contextConfigLocation,則利用該參數(shù)確定的配置文件,該參數(shù)指定的一個字符串,Spring的ContextLoaderListener負(fù)責(zé)將該字符串分解成多個配置文件,逗號“,”、空格“ ”、分號“;”都可作為字符串的分割符。
如果既然沒有applicationContext.xml文件,也沒有使用contextConfigLocation參數(shù)確定配置文件,或者contextConfigLocation確定的配置文件不存在,都將導(dǎo)致:Spring無法加載配置文件,無法正常創(chuàng)建ApplicationContext實例。
Spring根據(jù)bean定義創(chuàng)建 WebApplicationContext對象,并將其保存在Web應(yīng)用的ServletContext中。大部分情況下,應(yīng)用中的bean無需感受到ApplicationContext的存在,只要利用ApplicationContext的IoC即可。
如果需要在應(yīng)用中獲取ApplicationContext實例,可以通過如下方法獲取:
WebApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
下面是采用Servlet獲取ApplicationContext的完整源代碼:
public class SpringTestServlet extends HttpServlet
{
//Servlet的響應(yīng)方法。
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException,java.io.IOException
{z
//獲取Servlet的ServletContext對象
ServletContext sc = getServletContext();
//使用WebApplicationContextUtils類獲得ApplicationContext
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
//獲取Servlet的頁面輸出流
PrintWriter out = response.getWriter();
//將ApplicationContext對象輸出
out.println(ctx);
}
}
程序里手動獲取ApplicationContext對象,然后直接輸出到Servlet的響應(yīng)。結(jié)果看到,ApplicationContext加載了web.xml文件中指定的兩個配置文件。
二. 采用load-on-startup Servlet創(chuàng)建ApplicationContext
如果容器不支持Listener,則只能使用load-on-startup Servlet創(chuàng)建ApplicationContext實例,下面的容器都不支持Listener:
1.BEA WebLogic up to 8.1 SP2。
2.IBM WebSphere 5.x 。
3.Oracle OC4J 9.0.3。
Spring提供了一個特殊的Servlet類:ContextLoaderServlet。該Servlet在啟動時,會自動查找WEB-INF/下的applicationContext.xml文件。
當(dāng)然,為了讓ContextLoaderServlet隨應(yīng)用啟動而啟動,應(yīng)將此Servlet配置成load-on-startup的Servlet,load-on-startup的值小一點比較合適,因為要保證ApplicationContext優(yōu)先創(chuàng)建。如果只有一個配置文件,并且文件名為:applicationContext.xml。在web.xml文件中增加如下一段即可:
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
該Servlet用于提供“后臺”服務(wù),作為容器管理應(yīng)用中的其他bean,不需要響應(yīng)客戶請求,因此無需配置servlet-mapping。
如果有多個配置文件,一樣使用<context-param>元素來確定多個配置文件。事實上,不管是ContextLoaderServlet,還是ContextLoaderListener,都依賴于ContextLoader創(chuàng)建ApplicationContext實例。在ContextLoader代碼的第240行,有如下代碼:
String configLocation = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
if (configLocation != null) {
wac.setConfigLocations(StringUtils.tokenizeToStringArray(configLocation,
ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));
}
其中CONFIG_LOCATION_PARAM是該類的常量,其值為contextConfigLocation。可看出:ContextLoader首先檢查servletContext中是否有contextConfigLocation的參數(shù),如果有該參數(shù),則加載該參數(shù)指定的配置文件。帶多個配置文件的web.xml文件如下:
<!-- XML文件的文件頭-->
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- web.xml文件的DTD等信息-->
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!-- 確定多個配置文件-->
<context-param>
<!-- 參數(shù)名為contextConfigLocation -->
<param-name>contextConfigLocation</param-name>
<!-- 多個配置文件之間以,隔開 -->
<param-value>/WEB-INF/daoContext.xml,/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 采用load-on-startup Servlet創(chuàng)建ApplicationContext實例-->
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<!-- 下面值小一點比較合適,會優(yōu)先加載-->
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
測試所用的Servlet與前面所用的沒有區(qū)別。ContextLoaderServlet與ContextLoaderListener底層都依賴于ContextLoader。因此,二者的效果幾乎沒有區(qū)別。之間區(qū)別不是它們本身引起的,而是由于Servlet2.3的規(guī)范:listener比servlet優(yōu)先加載。因此,采用ContextLoaderListener創(chuàng)建ApplicationContext的時機更早。
當(dāng)然,也可以通過ServletContext的getAttribute方法獲取ApplicationContext,使用WebApplicationContextUtils類更便捷,因為無需記住ServletContext屬性名。即使ServletContext的WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE屬性沒有對應(yīng)對象,WebApplicationContextUtils的getWebApplicationContext()方法將會返回空,而不會引起異常。
獲得了WebApplicationContext實例的引用后,可以通過bean的名字訪問容器中的bean實例。大部分時候,無需通過這種方式訪問容器中的bean。將表現(xiàn)層的控制器bean置入容器的管理中,客戶端請求直接轉(zhuǎn)發(fā)給容器中的bean,然后由容器管理bean之間的依賴,因此,無需手動獲取ApplicationContext引用。當(dāng)然,每個框架都會有自己特定的整合策略。
posted on 2009-10-15 11:21
super_nini 閱讀(367)
評論(0) 編輯 收藏