SpringMVC配置多個properties文件之通配符解析
SpringMVC配置多個properties文件之通配符
在springmvc中配置加載properties文件一般會在
xml文件中配置如下
<context:property-placeholder location="classpath:resources/properties/zza.properties" ignore-unresolvable="true" />
如果希望在項目中添加了一個新的模塊,并且希望新的模塊和之前項目相對獨立,需要新添加一個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" />
多個SpringMVC項目配置統(tǒng)一管理
來自于springCloud的統(tǒng)一配置思路
因公司項目分多個系統(tǒng)進行開發(fā),而系統(tǒng)架構(gòu)幾乎完全一樣,所以同樣的配置文件會存在不同的系統(tǒng)中
當其中的某些配置需要修改時,就需要依次把所有系統(tǒng)中相關(guān)的配置都修改掉
純耗時且沒技術(shù)含量的體力活
所以借鑒SpringCloud的統(tǒng)一配置文件管理思想來對公司多個系統(tǒng)的配置文件也進行統(tǒng)一管理
1.首先是properties文件
針對諸如數(shù)據(jù)庫連接等類似的共通信息,如果數(shù)據(jù)庫信息發(fā)生變更則都需要修改,為了方便者直接在服務(wù)器上放置一個默認的連接配置
并發(fā)布到IIS等server上,通過http請求能夠獲取到
然后修改加載資源文件的配置文件如下:
<?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默認是支持http和file方式加載資源的
2.針對各類xml配置文件
項目中除了web.xml外,還有眾多的xml
和propertie文件一樣,也是相同的配置文件存在于不同的項目中,一改就要挨個改,煩
同理,將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>
這樣就可以直接啟動了,啟動時可以查看下面日志信息確定加載內(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)配費,找不到文件
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)
仔細看源碼 加載配置文件的源碼 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)}; } } }
思路都很簡單,配置的頭尾解析出目錄和含有通配符的文件,然后依次去找哪些文件滿足
不過很遺憾的是,如果是http開頭的通配符路徑,暫時是不支持的,支持classpth,jar等方式
不過讓人欣慰的是,是可以重寫文件加載方式的,原因很簡單,http目錄知道了,要知道目錄下面有哪些文件還是很簡單的(需要開啟iis的目錄瀏覽),然后取到所有文件后,如果和通配符匹配,則加載
雖然有遠端服務(wù)了,但是遠端服務(wù)只是一個默認的全局配置,
為了方便本地修改部分參數(shù)進行調(diào)試,所以在需要的時候,修改部分xml地址為classpath中的,只是在提交代碼的時候不要提交
若的確需要修改,則可以通知有服務(wù)器操作權(quán)限的人(我們公司比如我 ^_^)進行全局修改
以上僅為個人項目經(jīng)驗,其實就是把默認的classpath修改為了http,多思考,多總結(jié),多實踐,小改動,大用處。希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Java并發(fā)容器ConcurrentHashMap#put方法解析
下面小編就為大家?guī)硪黄贘ava并發(fā)容器ConcurrentHashMap#put方法解析。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06SpringMVC框架整合Junit進行單元測試(案例詳解)
本文詳細介紹在SpringMVC任何使用Junit框架。首先介紹了如何引入依賴,接著介紹了編寫一個測試基類,并且對其中涉及的各個注解做了一個詳細說明,感興趣的朋友跟隨小編一起看看吧2021-05-05IDEA+maven+SpringBoot+JPA+Thymeleaf實現(xiàn)Crud及分頁
這篇文章主要介紹了不需要電腦任何操作基于IDEA + maven + SpringBoot + JPA + Thymeleaf實現(xiàn)CRUD及分頁,需要的朋友可以參考下2018-03-03Spring?Boot項目中使用?TrueLicense?生成和驗證License的詳細步驟
這篇文章主要介紹了Spring?Boot項目中使用?TrueLicense?生成和驗證License,本文分步驟給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-10-10SpringBoot整合Mybatis注解開發(fā)的實現(xiàn)代碼
這篇文章主要介紹了SpringBoot整合Mybatis注解開發(fā)的實現(xiàn)代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Spring Cloud Config實現(xiàn)分布式配置中心
這篇文章主要介紹了Spring Cloud Config實現(xiàn)分布式配置中心,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04Maven方式構(gòu)建SpringBoot項目的實現(xiàn)步驟(圖文)
Maven是一個強大的項目管理工具,可以幫助您輕松地構(gòu)建和管理Spring Boot應(yīng)用程序,本文主要介紹了Maven方式構(gòu)建SpringBoot項目的實現(xiàn)步驟,具有一定的參考價值,感興趣的可以了解一下2023-09-09MyBatis JdbcType 與Oracle、MySql數(shù)據(jù)類型對應(yīng)關(guān)系說明
這篇文章主要介紹了MyBatis JdbcType 與Oracle、MySql數(shù)據(jù)類型對應(yīng)關(guān)系說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09