spring-mvc.xml 和 application-context.xml的區別


轉自:https://www.cnblogs.com/binlin1987/p/7053016.html
 
        
application-context.xml是全局的,應用於多個serverlet,配合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 是spring mvc的配置,web.xml中配置如下:
<!--配置springmvc DispatcherServlet-->
<servlet>
<servlet-name>springMVC</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這個一般是采用非spring mvc架構,用來加載Application Context。

如果直接采用SpringMVC,只需要把所有相關配置放到spring-mvc.xml中就好,一般spring mvc項目用不到多個serverlet

 

 

applicationContext.xml 配置文件在web.xml中的配置詳解

 

一、首先寫一下代碼結構。

 

二、再看web.xml中的配置情況。

復制代碼
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
            <param-name>contextConfigLocation</param-name>
            <!-- <param-value>classpath*:config/applicationContext.xml</param-value> -->
            <param-value>/WEB-INF/classes/config/applicationContext.xml</param-value>
  </context-param>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
     <servlet> 
        <servlet-name>springmvc</servlet-name> 
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
           <param-name>contextConfigLocation</param-name>
           <!-- <param-value>classpath*:config/Springmvc-servlet.xml</param-value> -->
           <param-value>/WEB-INF/classes/config/Springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup> 
    </servlet>  
    <servlet-mapping> 
        <servlet-name>springmvc</servlet-name> 
        <url-pattern>/</url-pattern>  
    </servlet-mapping>
</web-app>
復制代碼

 

三、下面是對配置文件的說明。

  <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>classpath*:config/applicationContext.xml</param-value> -->
            <param-value>/WEB-INF/classes/config/applicationContext.xml</param-value>
  </context-param>

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

這里需要搞清楚classpath是什么,以及classpath:和classpath*有何區別:

1. 首先 classpath是指 WEB-INF文件夾下的classes目錄

2. classpath 和 classpath* 區別: 
classpath:只會到你的class路徑中查找找文件; 
classpath*:不僅包含class路徑,還包括jar文件中(class路徑)進行查找. 

 

如果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>

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

復制代碼
    <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配置文件都放在一個統一的目錄下,也可以分模塊創建。


免責聲明!

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



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