新聞中心
小編給大家分享一下web.xml中classpath:和classpath*:有什么區(qū)別,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
公司主營(yíng)業(yè)務(wù):成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶(hù)真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。成都創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶(hù)帶來(lái)驚喜。成都創(chuàng)新互聯(lián)推出門(mén)頭溝免費(fèi)做網(wǎng)站回饋大家。
web.xml中classpath:和classpath*: 有什么區(qū)別?
classpath:只會(huì)到你的class路徑中查找找文件;
classpath*:不僅包含class路徑,還包括jar文件中(class路徑)進(jìn)行查找.
有時(shí)候會(huì)用模糊匹配的方式配置多配置文件。
但是如果配置文件是在jar包里,模糊匹配就找不到了??梢杂枚禾?hào)隔開(kāi)的方式配置多個(gè)配置文件。
如:
org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath*:applicationContext.xml, classpath*:app-datasource.xml, classpath*:app-memcached.xml, classpath*:app-ibatis.xml, classpath*:app-rest.xml
存放位置:
1:src下面
需要在web.xml中定義如下:
contextConfigLocation classpath:applicationContext.xml
2:WEB-INF下面
需要在web.xml中定義如下:
contextConfigLocation WEB-INF/applicationContext*.xml
web.xml 通過(guò)contextConfigLocation配置spring 的方式
SSI框架配置文件路徑問(wèn)題:
struts2的 1個(gè)+N個(gè) 路徑:src+src(可配置) 名稱(chēng): struts.xml + N
spring 的 1個(gè) 路徑: src 名稱(chēng): applicationContext.xml
ibatis 的 1個(gè)+N個(gè) 路徑: src+src(可配置) 名稱(chēng): SqlMapConfig.xml + N
部署到tomcat后,src目錄下的配置文件會(huì)和class文件一樣,自動(dòng)copy到應(yīng)用的 classes目錄下
spring的 配置文件在啟動(dòng)時(shí),加載的是web-info目錄下的applicationContext.xml,
運(yùn)行時(shí)使用的是web-info/classes目錄下的applicationContext.xml。
配置web.xml使這2個(gè)路徑一致:
contextConfigLocation /WEB-INF/classes/applicationContext.xml
多個(gè)配置文件的加載
contextConfigLocation 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
contextConfigLocation 參數(shù)定義了要裝入的 Spring 配置文件。
首先與Spring相關(guān)的配置文件必須要以"applicationContext-"開(kāi)頭,要符合約定優(yōu)于配置的思想,這樣在效率上和出錯(cuò)率上都要好很多。
還有最好把所有Spring配置文件都放在一個(gè)統(tǒng)一的目錄下,如果項(xiàng)目大了還可以在該目錄下分模塊建目錄。這樣程序看起來(lái)不會(huì)很亂。
在web.xml中的配置如下:
Xml代碼
< param-name>contextConfigLocation < param-value>classpath*:**/applicationContext-*.xml < /context-param>
"**/"表示的是任意目錄;
"**/applicationContext-*.xml"表示任意目錄下的以"applicationContext-"開(kāi)頭的XML文件。
你自己可以根據(jù)需要修改。最好把所有Spring配置文件都放在一個(gè)統(tǒng)一的目錄下,如:
contextConfigLocation classpath:/spring/applicationContext-*.xml
然后從Web系統(tǒng)中的XML文件來(lái)載入Bean定義的信息:
ServletContext servletContext = request.getSession().getServletContext(); ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
以上是“web.xml中classpath:和classpath*:有什么區(qū)別”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享標(biāo)題:web.xml中classpath:和classpath*:有什么區(qū)別
URL網(wǎng)址:http://ef60e0e.cn/article/podcho.html