框架学习随笔3-SpringBoot profile环境配置

The spring.profiles.active property(属性) follows the same ordering rules as other properties: The highest PropertySource wins. This means that you can specify(明确指定) active profiles in application.properties and then replace them by using the command line switch.

Sometimes, it is useful to have profile-specific properties that add to the active profiles rather than replace them. The spring.profiles.include property can be used to unconditionally(无条件地) add active profiles. The SpringApplication entry point also has a Java API for setting additional profiles (that is, on top of those activated by the spring.profiles.active property). See the setAdditionalProfiles() method in SpringApplication.

For example, when an application with the following properties is run by using the switch(转换), --spring.profiles.active=prod, the proddb and prodmq profiles are also activated:

---

my.property: fromyamlfile

---

spring.profiles:

 prod spring.profiles.include: 

  - proddb

  - prodmq

不同的环境下,会有不同的配置。

applcation.properties下可以使用下面的激活环境配置:

spring.profiles.active = dev

一般默认,dev为开发环境、test为测试环境、prod为生产环境。可以将对应的配置文件命名为application-dev.properties、application-prod.properties、application-test.properties。

profile.name=dev
profile.name=test
profile.name=prod

applcation.yml可以使用下面的激活环境配置。applcation.yml支持以“---”分割的多文档块方式。

spring:
  profiles:
    active: dev

---

spring:
  profiles:
    active: test

---

spring:
  profiles:
    active: prod

 配置文件之间存在互补配置。

加载顺序可以根据配置文件存放的路径来看,从上到下,优先级从高到低。前两者在程序包下,后两者在java文件夹下:

  • file:./config/
  • file:./
  • classpath:./config/
  • classpath:./

外部配置加载的顺序,首先是命令行参数,然后由jar包外向内,优先加载带spring.profiles的配置文件。

profile参考文档地址:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-profiles

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值