轉自http://www.cnblogs.com/cczz_11/p/4363314.html html
https://blog.csdn.net/u013743160/article/details/52734017java
1、首先寫一下代碼結構。web
2、再看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>
3、下面是對配置文件的說明。
- <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配置文件都放在一個統一的目錄下,也能夠分模塊建立。