web.xml 配置applicationContext.xml

來源:互聯網
上載者:User

標籤:style   color   使用   io   檔案   ar   問題   cti   

web.xml中classpath:和classpath*:  有什麼區別? 

classpath:只會到你的class路徑中尋找找檔案; 
classpath*:不僅包含class路徑,還包括jar檔案中(class路徑)進行尋找.


 有時候會用模糊比對的方式配置多設定檔。

但是如果設定檔是在jar包裡,模糊比對就找不到了。可以用逗號隔開的方式配置多個設定檔。


如:

<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath*:applicationContext.xml,          classpath*:app-datasource.xml,          classpath*:app-memcached.xml,          classpath*:app-ibatis.xml,          classpath*:app-rest.xml      </param-value>  </context-param>

存放位置:
1:src下面
需要在web.xml中定義如下:

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

2:WEB-INF下面
需要在web.xml中定義如下:

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

web.xml 通過contextConfigLocation配置spring 的方式 
SSI架構設定檔路徑問題: 

struts2的 1個+N個 路徑:src+src(可配置) 名稱: struts.xml + N 
spring 的 1個 路徑: src 名稱: applicationContext.xml
ibatis 的 1個+N個 路徑: src+src(可配置) 名稱: SqlMapConfig.xml + N 


部署到tomcat後,src目錄下的設定檔會和class檔案一樣,自動copy到應用的 classes目錄下 

spring的 設定檔在啟動時,載入的是web-info目錄下的applicationContext.xml, 
運行時使用的是web-info/classes目錄下的applicationContext.xml。 

配置web.xml使這2個路徑一致: 

<context-param>   <param-name>contextConfigLocation</param-name>   <param-value>/WEB-INF/classes/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,   classpath*:conf/spring/applicationContext_staff*.xml,   classpath*:conf/spring/applicationContext_security.xml   classpath*:conf/spring/applicationContext_modules*.xml   classpath*:conf/spring/applicationContext_cti*.xml   classpath*:conf/spring/applicationContext_apm*.xml   </param-value>   </context-param>

   

contextConfigLocation 參數定義了要裝入的 Spring 設定檔。 

首先與Spring相關的設定檔必須要以"applicationContext-"開頭,要符合約定優於配置的思想,這樣在效率上和出錯率上都要好很多。 
還有最好把所有Spring設定檔都放在一個統一的目錄下,如果項目大了還可以在該目錄下分模組建目錄。這樣程式看起來不會很亂。 
在web.xml中的配置如下: 
Xml代碼 

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

"**/"表示的是任意目錄; 
"**/applicationContext-*.xml"表示任意目錄下的以"applicationContext-"開頭的XML檔案。 
你自己可以根據需要修改。最好把所有Spring設定檔都放在一個統一的目錄下,如: 

<!-- Spring 的配置 --> 

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

  


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.