spring profile多配置文件的切换

老生常谈的问题 开发环境 测试环境 生产环境分别对应的properties配置文件

在最少改动代码的基础上进行灵活的切换

三个配置文件分别为 local.properties,test.properties,production.properties

1.在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"

好了,大功告成





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值