這篇文章將為大家詳細講解有關SpringMVC配置多個properties文件之通配符的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
在springmvc中配置加載properties文件一般會在
<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屬性設置為true。
具體如下:
<context:property-placeholder location="classpath*:resources/properties/*.properties" />
來自于springCloud的統一配置思路
因公司項目分多個系統進行開發,而系統架構幾乎完全一樣,所以同樣的配置文件會存在不同的系統中
當其中的某些配置需要修改時,就需要依次把所有系統中相關的配置都修改掉
純耗時且沒技術含量的體力活
所以借鑒SpringCloud的統一配置文件管理思想來對公司多個系統的配置文件也進行統一管理
針對諸如數據庫連接等類似的共通信息,如果數據庫信息發生變更則都需要修改,為了方便者直接在服務器上放置一個默認的連接配置
并發布到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方式加載資源的
項目中除了web.xml外,還有眾多的xml

和propertie文件一樣,也是相同的配置文件存在于不同的項目中,一改就要挨個改,煩
同理,將xml發布,并修改IIS設置,使其通過瀏覽器能訪問
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>
這樣就可以直接啟動了,啟動時可以查看下面日志信息確定加載內容是正確的

最開始是修改為這樣的
<context-param> <param-name>contextConfigLocation</param-name> <param-value> http://192.168.0.32:82/springConfig/applicationContext-*.xml </param-value> </context-param>
和classpath一樣,但是很遺憾,解析不了統配費,找不到文件
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的目錄瀏覽),然后取到所有文件后,如果和通配符匹配,則加載
雖然有遠端服務了,但是遠端服務只是一個默認的全局配置,
為了方便本地修改部分參數進行調試,所以在需要的時候,修改部分xml地址為classpath中的,只是在提交代碼的時候不要提交
若的確需要修改,則可以通知有服務器操作權限的人進行全局修改
關于“SpringMVC配置多個properties文件之通配符的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。