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 按照以下順序加載配置,該順序旨在允許合理地覆蓋值

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章