Spring profile 多环境配置 简记

  • 根据profile划分组,用于配置开发环境,测试环境,生产环境
  • 在spring中多环境配置需满足application-{profile}.properties格式,比如application-dev.properties:开发环境
    • springboot可以通过在配置文件中设置spring.profiles.active和spring.profiles.default属性来设置对应的生效环境,所以可以同时生效多个环境
    • spring 只要在applicationContext.xml 中添加在不同环境下加载的配置文件即可,例如:
<!-- 开发环境配置文件 -->
         <beans profile="dev">
    <context:property-placeholder location="classpath:application-dev.properties" />
  </beans>
  • 需要注意:profile的定义记得放在文档的最下边
  • spring提供许多激活方式:
    1. 系统环境变量的方式:通过配置系统环境变量激活profile,spring.profile.default=dev或者ConfigurableEnvironment.setActiveProfiles("test")
    2. JVM参数方式:在容器添加JAVA_OPS,如:set JAVA_OPTS="-Dspring.profiles.active=dev"
    3. web.xml的方式:
    <init-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>production</param-value>
    </init-param>
    4. 注释方式:@ActiveProfiles({"unittest","devprofile"})
    5. 以上顺序关系激活的优先级别,顶上级别最高
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值