多個SpringMVC項目配置統一管理(來自於springCloud的統一配置思路)


因公司項目分多個系統進行開發,而系統架構幾乎完全一樣,所以同樣的配置文件會存在不同的系統中

當其中的某些配置需要修改時,就需要依次把所有系統中相關的配置都修改掉

純耗時且沒技術含量的體力活

所以借鑒SpringCloud的統一配置文件管理思想來對公司多個系統的配置文件也進行統一管理

1.首先是properties文件

  針對諸如數據庫連接等類似的共通信息,如果數據庫信息發生變更則都需要修改,為了方便者直接在服務器上放置一個默認的連接配置

  並發布到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發布,並修改IIS設置,使其通過瀏覽器能訪問
iis需要增加MIME類型 properties和xml為text/plain才能在瀏覽器訪問

然后就可以在瀏覽器訪問了

然后修改web.xml加載文件的地方如下:
<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中的,只是在提交代碼的時候不要提交

若的確需要修改,則可以通知有服務器操作權限的人(我們公司比如我 ^_^)進行全局修改

 

以上僅為個人項目經驗,其實就是把默認的classpath修改為了http,

 

 

多思考,多總結,多實踐,小改動,大用處

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM