老生常談的問題 開發(fā)環(huán)境 測試環(huán)境 生產(chǎn)環(huán)境分別對應(yīng)的PRoperties配置文件
在最少改動代碼的基礎(chǔ)上進(jìn)行靈活的切換
三個配置文件分別為 local.properties,test.properties,production.properties
1.在spring的配置文件里加上代碼
<!-- 測試環(huán)境配置文件 --> <beans profile="test"> <context:property-placeholder location="classpath:test.properties" /> <util:properties id="APP_PROP" location="classpath:test.properties" local-override="true"/> </beans> <!-- 本地環(huán)境配置文件 --> <beans profile="local"> <context:property-placeholder location="classpath:local.properties" /> <util:properties id="APP_PROP" location="classpath:local.properties" local-override="true"/> </beans>
<!-- 生產(chǎn)環(huán)境配置文件 --> <beans profile="local"> <context:property-placeholder location="classpath:production.properties" /> <util:properties id="APP_PROP" location="classpath:production.properties" local-override="true"/> </beans>
util:properties是用@Value注解來引入配置文件中的變量
2.然后在web.xml文件中設(shè)置默認(rèn)指向(我這里指向的是local)
<context-param> <param-name>spring.profiles.default</param-name> <param-value>local</param-value> </context-param>
3.然后就是激活你想要的配置文件
這里網(wǎng)上有很多方法 比如在web.xml文件里加入
<context-param> <param-name>spring.profiles.default</param-name> <param-value>local</param-value> </context-param>
考慮到盡量少的改動代碼,我這里在
tomcat 的啟動腳本中加入以下 JVM 參數(shù) 來激活
-Dspring.profiles.active="xxx"本地開發(fā)環(huán)境用默認(rèn)的local測試環(huán)境的tomcat啟動腳本中加入-Dspring.profiles.active="test"生產(chǎn)環(huán)境的tomcat啟動腳本中加入-Dspring.profiles.active="production"好了,大功告成
新聞熱點
疑難解答