springboot(二)多文件配置的激活与加载顺序

目录

一、多Profile文件

1.文件名 application-{profile}.properties/yml

2.默认使用application.properties/yml 的配置

3.激活 application-{profile}.properties/yml

4.或 Idea Configuration配置以下两项其中一项

5.两者都配置:Program arguments 优先于 VM options

6.或启动时指定配置(最高优先级)

二、yml多文档块方式

三、配置文件的加载位置与顺序

1.默认加载

2.指定 spring.config.location

3.指定 spring.config.additional-location

4.指定 spring.config.name=testName

四、外部配置加载顺序


一、多Profile文件

1.文件名 application-{profile}.properties/yml

2.默认使用application.properties/yml 的配置

3.激活 application-{profile}.properties/yml

spring:
  profiles:
    active: dev

4.或 Idea Configuration配置以下两项其中一项

VM options:

-Dspring.profiles.active=dev

Program arguments:

--spring.profiles.active=prod

5.两者都配置:Program arguments 优先于 VM options

6.或启动时指定配置(最高优先级)

java -jar springboot-learning-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

 

 

 

二、yml多文档块方式

server:
  port: 8080
spring:
  profiles:
    active: prod

---
server:
  port: 8083
spring:
  profiles: dev


---
server:
  port: 8084
spring:
  profiles: prod

 

三、配置文件的加载位置与顺序

1.默认加载

springboot 启动从 application.properties/yml 以下位置的文件加载属性,全部位置都会扫描,并且按列表优先级排序(不同的属性会互补配置,相同的属性在列表较高位置定义的属性会覆盖较低位置定义的属性)

按照列表顺序的四个文件

server:
  port: 8080

hello-world: project_config_helloWorld



server:
  port: 8080

hello-world: project_helloWorld



server:
  port: 8080

hello-world: classpath_config_helloWorld



server:
  port: 8080

hello-world: classpath_helloWorld
hello-world2: classpath_helloWorld2

 

 

 

2.指定 spring.config.location

只会加载指定的文件,并且优先顺序为后面覆盖前面的

--spring.config.location=classpath:/config/application.yml,classpath:/application.yml

3.指定 spring.config.additional-location

当使用spring.config.additional-location指定配置文件(除默认4个位置外)。例如,classpath:/custom-config/,file:./custom-config/配置了的其他位置,则搜索顺序变为以下内容

  • file:./custom-config/
  • classpath:custom-config/
  • file:./config/
  • file:./
  • classpath:/config/
  • classpath:/

4.指定 spring.config.name=testName

--spring.config.name=testName

四、外部配置加载顺序

Spring Boot 按照以下顺序加载配置,该顺序旨在允许合理地覆盖值

 

 

 

 

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值