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

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

轉自http://www.cnblogs.com/cczz_11/p/4363314.html html

https://blog.csdn.net/u013743160/article/details/52734017java

 

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





2、再看web.xml中的配置狀況。
[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   <display-name>SpringMVC</display-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.html</welcome-file>  
  6.     <welcome-file>index.htm</welcome-file>  
  7.     <welcome-file>index.jsp</welcome-file>  
  8.     <welcome-file>default.html</welcome-file>  
  9.     <welcome-file>default.htm</welcome-file>  
  10.     <welcome-file>default.jsp</welcome-file>  
  11.   </welcome-file-list>  
  12.   <context-param>  
  13.             <param-name>contextConfigLocation</param-name>  
  14.             <!-- <param-value>classpath*:config/applicationContext.xml</param-value> -->  
  15.             <param-value>/WEB-INF/classes/config/applicationContext.xml</param-value>  
  16.   </context-param>  
  17.     
  18.   <listener>  
  19.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  20.   </listener>  
  21.      <servlet>   
  22.         <servlet-name>springmvc</servlet-name>   
  23.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  24.         <init-param>  
  25.            <param-name>contextConfigLocation</param-name>  
  26.            <!-- <param-value>classpath*:config/Springmvc-servlet.xml</param-value> -->  
  27.            <param-value>/WEB-INF/classes/config/Springmvc-servlet.xml</param-value>  
  28.         </init-param>  
  29.         <load-on-startup>1</load-on-startup>   
  30.     </servlet>    
  31.     <servlet-mapping>   
  32.         <servlet-name>springmvc</servlet-name>   
  33.         <url-pattern>/</url-pattern>    
  34.     </servlet-mapping>  
  35. </web-app>  
 


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

[html]  view plain  copy
 
  1. <listener>  
  2.   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  3. </listener>  


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

[html]  view plain  copy
 
  1. <context-param>  
  2.           <param-name>contextConfigLocation</param-name>  
  3.           <!-- <param-value>classpath*:config/applicationContext.xml</param-value> -->  
  4.           <param-value>/WEB-INF/classes/config/applicationContext.xml</param-value>  
  5. </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中的配置就以下所示:

[html]  view plain  copy
 
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>classpath:applicationContext.xml</param-value>  
  4. </context-param>  


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

[html]  view plain  copy
 
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>WEB-INF/applicationContext*.xml</param-value>  
  4. </context-param>  


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

[html]  view plain  copy
 
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>WEB-INF/applicationContext*.xml</param-value>  
  4. </context-param>  


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


複製代碼
[html]  view plain  copy
 
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>   
  4.         classpath*:conf/spring/applicationContext_core*.xml,   
  5.         classpath*:conf/spring/applicationContext_dict*.xml,  
  6.         classpath*:conf/spring/applicationContext_hibernate.xml,  
  7.         ......  
  8.     </param-value>  
  9. </context-param>  


複製代碼
也能夠用下面的這種方式:
[html]  view plain  copy
 
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>classpath*:**/applicationContext-*.xml</param-value>  
  4. </context-param>  


"**/"表示的是任意目錄; 


"**/applicationContext-*.xml"表示任意目錄下的以"applicationContext-"開頭的XML文件。 


Spring配置文件最好以"applicationContext-"開頭,且最好把全部Spring配置文件都放在一個統一的目錄下,也能夠分模塊建立。
文章標籤: spring
 
在web.xml中如何加載aplicationContext.xml文件

web.xml的寫法spring

[html]  view plain  copy
 
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     version="2.5">  
  5.   
  6.     <display-name>demo-web</display-name>  
  7.       
  8.     <!-- Spring ApplicationContext配置文件的路徑,可以使用通配符,多個路徑用,號分隔  
  9.         此參數用於後面的Spring Context Loader -->  
  10.     <context-param>  
  11.         <param-name>contextConfigLocation</param-name>  
  12.         <param-value>  
  13.             classpath*:/applicationContext.xml  
  14.             classpath*:/applicationContext-shiro.xml  
  15.         </param-value>  
  16.     </context-param>  
  17.   
  18.     <!--Spring的ApplicationContext 載入 -->  
  19.     <listener>  
  20.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  21.     </listener>  
  22.   
  23. </web-app></span

 

 

 

applicationContext.xml的兩種加載方式

第一種:直接將之放到/WEB-INF下,在web.xml中聲明一個listener;服務器

<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
  • 1
  • 2
  • 3
  • 4
默認的路徑是/WEB-INF/applicationContext.xml
  • 1
  • 2

第二種:將之放到classpath下,可是此時要在web.xml中加入,用它來指明你的applicationContext.xml的位置以供web容器來加載。按照Struts2 整合spring的官方給出的檔案,寫成:markdown

<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml </param-value> </context-param>
相關文章
相關標籤/搜索