欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringMVC配置多個(gè)properties文件之通配符解析

 更新時(shí)間:2021年09月08日 10:02:48   作者:朝著希望前進(jìn)  
這篇文章主要介紹了SpringMVC配置多個(gè)properties文件之通配符解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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)文章

最新評(píng)論