SpringMVC配置多個(gè)properties文件之通配符解析
SpringMVC配置多個(gè)properties文件之通配符
在springmvc中配置加載properties文件一般會(huì)在
xml文件中配置如下
<context:property-placeholder location="classpath:resources/properties/zza.properties" ignore-unresolvable="true" />
如果希望在項(xiàng)目中添加了一個(gè)新的模塊,并且希望新的模塊和之前項(xiàng)目相對(duì)獨(dú)立,需要新添加一個(gè)properties文件的話,那么需要在xml配置文件中,再配置一份。比如:
<context:property-placeholder location="classpath:resources/properties/zza.properties" ignore-unresolvable="true" /> <context:property-placeholder location="classpath:resources/properties/weixin.properties" ignore-unresolvable="true" />
這樣做就太麻煩了,每次添加完properties文件還得在xml文件中添加。并且還必須把ignore-unresolvable屬性設(shè)置為true。
解決方案是:利用通配符
具體如下:
<context:property-placeholder location="classpath*:resources/properties/*.properties" />
多個(gè)SpringMVC項(xiàng)目配置統(tǒng)一管理
來自于springCloud的統(tǒng)一配置思路
因公司項(xiàng)目分多個(gè)系統(tǒng)進(jìn)行開發(fā),而系統(tǒng)架構(gòu)幾乎完全一樣,所以同樣的配置文件會(huì)存在不同的系統(tǒng)中
當(dāng)其中的某些配置需要修改時(shí),就需要依次把所有系統(tǒng)中相關(guān)的配置都修改掉
純耗時(shí)且沒技術(shù)含量的體力活
所以借鑒SpringCloud的統(tǒng)一配置文件管理思想來對(duì)公司多個(gè)系統(tǒng)的配置文件也進(jìn)行統(tǒng)一管理
1.首先是properties文件
針對(duì)諸如數(shù)據(jù)庫連接等類似的共通信息,如果數(shù)據(jù)庫信息發(fā)生變更則都需要修改,為了方便者直接在服務(wù)器上放置一個(gè)默認(rèn)的連接配置
并發(fā)布到IIS等server上,通過http請(qǐng)求能夠獲取到
然后修改加載資源文件的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>http://192.168.0.32:82/properties/jdbc.properties</value> </list> </property> </bean> </beans>
PropertyPlaceholderConfigurer默認(rèn)是支持http和file方式加載資源的
2.針對(duì)各類xml配置文件
項(xiàng)目中除了web.xml外,還有眾多的xml
和propertie文件一樣,也是相同的配置文件存在于不同的項(xiàng)目中,一改就要挨個(gè)改,煩
同理,將xml發(fā)布,并修改IIS設(shè)置,使其通過瀏覽器能訪問
iis需要增加MIME類型 properties和xml為text/plain才能在瀏覽器訪問
然后就可以在瀏覽器訪問了
<context-param> <param-name>contextConfigLocation</param-name> <param-value> http://192.168.0.32:82/springConfig/applicationContext-resource.xml, http://192.168.0.32:82/springConfig/applicationContext-db.xml, http://192.168.0.32:82/springConfig/applicationContext-redis.xml, http://192.168.0.32:82/springConfig/applicationContext-redission.xml, http://192.168.0.32:82/springConfig/applicationContext-service.xml, http://192.168.0.32:82/springConfig/applicationContext-filter.xml </param-value> </context-param>
<servlet> <description>spring-mvc</description> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> http://192.168.0.32:82/spring-mvc.xml <!-- classpath:spring-mvc.xml --> </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
這樣就可以直接啟動(dòng)了,啟動(dòng)時(shí)可以查看下面日志信息確定加載內(nèi)容是正確的
最開始是修改為這樣的
<context-param> <param-name>contextConfigLocation</param-name> <param-value> http://192.168.0.32:82/springConfig/applicationContext-*.xml </param-value> </context-param>
和classpath一樣,但是很遺憾,解析不了統(tǒng)配費(fèi),找不到文件
java.io.FileNotFoundException: URL [http://192.168.0.32:82/springConfig/] cannot be resolved to absolute file path because it does not reside in the file system: http://192.168.0.32:82/springConfig/
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:215)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:53)
at org.springframework.core.io.UrlResource.getFile(UrlResource.java:213)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.doFindPathMatchingFileResources(PathMatchingResourcePatternResolver.java:689)
at org.springframework.web.context.support.ServletContextResourcePatternResolver.doFindPathMatchingFileResources(ServletContextResourcePatternResolver.java:92)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.findPathMatchingResources(PathMatchingResourcePatternResolver.java:478)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.getResources(PathMatchingResourcePatternResolver.java:293)
仔細(xì)看源碼 加載配置文件的源碼 PathMatchingResourcePatternResolver中這段
@Override public Resource[] getResources(String locationPattern) throws IOException { Assert.notNull(locationPattern, "Location pattern must not be null"); if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) { // a class path resource (multiple resources for same name possible) if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) { // a class path resource pattern return findPathMatchingResources(locationPattern); } else { // all class path resources with the given name return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length())); } } else { // Generally only look for a pattern after a prefix here, // and on Tomcat only after the "*/" separator for its "war:" protocol. int prefixEnd = (locationPattern.startsWith("war:") ? locationPattern.indexOf("*/") + 1 : locationPattern.indexOf(":") + 1); if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) { // a file pattern return findPathMatchingResources(locationPattern); } else { // a single resource with the given name return new Resource[] {getResourceLoader().getResource(locationPattern)}; } } }
思路都很簡(jiǎn)單,配置的頭尾解析出目錄和含有通配符的文件,然后依次去找哪些文件滿足
不過很遺憾的是,如果是http開頭的通配符路徑,暫時(shí)是不支持的,支持classpth,jar等方式
不過讓人欣慰的是,是可以重寫文件加載方式的,原因很簡(jiǎn)單,http目錄知道了,要知道目錄下面有哪些文件還是很簡(jiǎn)單的(需要開啟iis的目錄瀏覽),然后取到所有文件后,如果和通配符匹配,則加載
雖然有遠(yuǎn)端服務(wù)了,但是遠(yuǎn)端服務(wù)只是一個(gè)默認(rèn)的全局配置,
為了方便本地修改部分參數(shù)進(jìn)行調(diào)試,所以在需要的時(shí)候,修改部分xml地址為classpath中的,只是在提交代碼的時(shí)候不要提交
若的確需要修改,則可以通知有服務(wù)器操作權(quán)限的人(我們公司比如我 ^_^)進(jìn)行全局修改
以上僅為個(gè)人項(xiàng)目經(jīng)驗(yàn),其實(shí)就是把默認(rèn)的classpath修改為了http,多思考,多總結(jié),多實(shí)踐,小改動(dòng),大用處。希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Java并發(fā)容器ConcurrentHashMap#put方法解析
下面小編就為大家?guī)硪黄贘ava并發(fā)容器ConcurrentHashMap#put方法解析。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06SpringMVC框架整合Junit進(jìn)行單元測(cè)試(案例詳解)
本文詳細(xì)介紹在SpringMVC任何使用Junit框架。首先介紹了如何引入依賴,接著介紹了編寫一個(gè)測(cè)試基類,并且對(duì)其中涉及的各個(gè)注解做了一個(gè)詳細(xì)說明,感興趣的朋友跟隨小編一起看看吧2021-05-05IDEA+maven+SpringBoot+JPA+Thymeleaf實(shí)現(xiàn)Crud及分頁
這篇文章主要介紹了不需要電腦任何操作基于IDEA + maven + SpringBoot + JPA + Thymeleaf實(shí)現(xiàn)CRUD及分頁,需要的朋友可以參考下2018-03-03Spring?Boot項(xiàng)目中使用?TrueLicense?生成和驗(yàn)證License的詳細(xì)步驟
這篇文章主要介紹了Spring?Boot項(xiàng)目中使用?TrueLicense?生成和驗(yàn)證License,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-10-10SpringBoot整合Mybatis注解開發(fā)的實(shí)現(xiàn)代碼
這篇文章主要介紹了SpringBoot整合Mybatis注解開發(fā)的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Spring Cloud Config實(shí)現(xiàn)分布式配置中心
這篇文章主要介紹了Spring Cloud Config實(shí)現(xiàn)分布式配置中心,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04Maven方式構(gòu)建SpringBoot項(xiàng)目的實(shí)現(xiàn)步驟(圖文)
Maven是一個(gè)強(qiáng)大的項(xiàng)目管理工具,可以幫助您輕松地構(gòu)建和管理Spring Boot應(yīng)用程序,本文主要介紹了Maven方式構(gòu)建SpringBoot項(xiàng)目的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09spring 定時(shí)任務(wù)@Scheduled詳解
這篇文章主要介紹了spring 定時(shí)任務(wù)@Scheduled的相關(guān)資料,文中通過示例代碼介紹的很詳細(xì),相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值,有需要的朋友們下面來一起看看吧。2017-01-01MyBatis JdbcType 與Oracle、MySql數(shù)據(jù)類型對(duì)應(yīng)關(guān)系說明
這篇文章主要介紹了MyBatis JdbcType 與Oracle、MySql數(shù)據(jù)類型對(duì)應(yīng)關(guān)系說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09