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

本文介绍如何在不将配置文件打包进WAR的情况下,通过修改web.xml来实现Spring和Log4j配置文件的外部加载。该方法适用于希望灵活配置部署路径的应用程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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

转载于:https://www.cnblogs.com/lwmp/p/6221829.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值