spring webapp的配置文件放置在项目外的方法

在web.xml中,填写

    <context-param>
        <param-name>CFG_HOME</param-name>
        <param-value>file:///D:/cfg</param-value>
    </context-param>
在spring配置文件中
<context:property-placeholder location="${CFG_HOME}/some.properties"  ignore-unresolvable="true"/>
另外,如果想把log4j.properties也放在项目外的话
需要添加一个listener到其他listener的前面
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
并添加一个参数
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>file:///D:/cfg/log4j.properties</param-value>
    </context-param>
这样导出的war就不用放置任何的配置文件了,仅仅需要修改web.xml中两处存放properties文件的路径就可以了,一处用于spring,一处用于log4j。
 
参考
 
 
这种方法还不是特别好,项目中采用的时maven profile
posted on 2016-12-26 11:51  斜月三星一太阳  阅读(219)  评论(0编辑  收藏  举报