您的位置:首页 > 编程语言 > Java开发

spring profile多配置文件的切换

2017-02-09 14:31 225 查看
老生常谈的问题 开发环境 测试环境 生产环境分别对应的properties配置文件在最少改动代码的基础上进行灵活的切换三个配置文件分别为 local.properties,test.properties,production.properties1.在spring的配置文件里加上代码<!-- 测试环境配置文件 -->    <beans profile="test">        <context:property-placeholder location="classpath:test.properties" />         <util:properties id="APP_PROP" location="classpath:test.properties" local-override="true"/>    </beans>    <!-- 本地环境配置文件 -->    <beans profile="local">        <context:property-placeholder location="classpath:local.properties" />         <util:properties id="APP_PROP" location="classpath:local.properties" local-override="true"/>    </beans>    <!-- 生产环境配置文件 -->    <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文件中设置默认指向(我这里指向的是local)<context-param>           <param-name>spring.profiles.default</param-name>           <param-value>local</param-value>      </context-param> 3.然后就是激活你想要的配置文件这里网上有很多方法 比如在web.xml文件里加入<context-param>           <param-name>spring.profiles.default</param-name>           <param-value>local</param-value>      </context-param> 考虑到尽量少的改动代码,我这里在tomcat 的启动脚本中加入以下 JVM参数 来激活
-Dspring.profiles.active="xxx"
本地开发环境用默认的local
测试环境的tomcat启动脚本中加入
-Dspring.profiles.active="test"
生产环境的tomcat启动脚本中加入
-Dspring.profiles.active="production"
好了,大功告成

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐