一、需求
爲了使項目配置不受生產、測試、開發環境的影響,且更加可視化可維護配置文件,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