spring-mvc.xml與applicationContext.xml的區別

1.用途不同

applicationContext.xml文件通常用於加載spring系統級別的組件,比如bean的初始化。
spring-mvc.xml文件通常用於加載controller層需要的類,比如攔截器,mvc標籤加載的類。

2.加載位置不同

applicationContext.xml加載在標籤中,作爲FrameworkServlet的參數屬性。
spring-mvc.xml文件當做DispatcherServlet的參數屬性進行加載。

applicationContext.xml是全局的,應用於多個servelet,配合listener一起使用。
web.xml中配置如下:

<!-- 配置監聽器 -->
<listener>        
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

spring-mvc.xml是SpringMVC的配置,web.xml中配置如下:

<!--配置SpringMVC DispatcherServlet-->
<servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/spring-mvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  <async-supported>true</async-supported>
</servlet>

application-context.xml這個一般是採用非SpringMVC架構,用來加載Application Context。
如果直接採用SpringMVC,只需要把所有相關配置放到spring-mvc.xml中就好,一般SpringMVC項目用不到多個serverlet。

下面是對web.xml配置文件的說明:

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

ContextLoaderListener是Spring的監聽器,它的作用就是啓動Web容器時,自動裝配ApplicationContext的配置信息。因爲它實現了ServletContextListener這個接口,在web.xml配置這個監聽器,啓動容器時,就會默認執行它實現的方法。

<context-param>
     <param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/config/applicationContext.xml</param-value>
     <!--<param-value>classpath*:config/applicationContext.xml</param-value> -->       
</context-param>

這段配置是用於指定applicationContext.xml配置文件的位置,可通過context-param加以指定。

如果applicationContext.xml配置文件存放在src目錄下,那麼在web.xml中的配置就如下所示:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

如果applicationContext.xml配置文件存放在WEB-INF下面,那麼在web.xml中的配置就如下所示:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/applicationContext*.xml</param-value>
</context-param>

需要注意的是,部署到應用服務器後,src目錄下的配置文件會和class文件一樣,自動copy到應用的classes目錄下,spring的配置文件在啓動時,加載的是web-info目錄下的applicationContext.xml運行時使用的是web-info/classes目錄下的applicationContext.xml。因此,不管applicationContext.xml配置文件存放在src目錄下,還是存放在WEB-INF下面,都可以用下面這種方式來配置路徑:

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>WEB-INF/applicationContext*.xml</param-value>
</context-param>

注意:
1.classpath是指 WEB-INF文件夾下的classes目錄
2.classpath與classpath的區別:
classpath:指classes路徑下文件
classpath
:除了classes路徑下文件,還包含jar包中文件


當有多個配置文件加載時,可採用下面代碼來配置:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value> 
         classpath*:conf/spring/applicationContext_core*.xml, 
         classpath*:conf/spring/applicationContext_dict*.xml,
         classpath*:conf/spring/applicationContext_hibernate.xml,
         ......
    </param-value>
 </context-param>

也可以用下面的這種方式:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:**/applicationContext-*.xml</param-value>
</context-param>

注:
"/"表示的是任意目錄;
"
/applicationContext-*.xml"表示任意目錄下的以"applicationContext-"開頭的XML文件。
Spring配置文件最好以"applicationContext-"開頭,且最好把所有Spring配置文件都放在一個統一的目錄下,也可以分模塊創建。

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