目录
1.文件名 application-{profile}.properties/yml
2.默认使用application.properties/yml 的配置
3.激活 application-{profile}.properties/yml
4.或 Idea Configuration配置以下两项其中一项
5.两者都配置:Program arguments 优先于 VM options
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 按照以下顺序加载配置,该顺序旨在允许合理地覆盖值