一、需求
为了使项目配置不受生产、测试、开发环境的影响,且更加可视化可维护配置文件,web项目的配置文件需要根据不同环境配置动态变更,不能直接写死在war中,就需要我们实现war包可以读取外部的配置文件。
二、准备
windows、eclipse、tomcat、spring配置
三、实现
1、在war包外新建配置文件,如下图
2、修改tomcat conf/catalina.properties 将配置文件路径加入common.loader
3、在spring.xml配置中新加:
<!-- 加载包外配置 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="location" value="file:E:/cms_dabao/config/spring_config.properties"/>
</bean>
4、在spring.xml配置中引用包外的配置文件spring_config.properties
5、clean项目,重新启动,over
文档参考:https://blog.csdn.net/Mars93/article/details/78017209
https://blog.csdn.net/evilcry2012/article/details/51248513