SpringBoot詳細講解多個配置文件的配置流程
一般情況下,springboot默認會在resource目錄下生成一個配置文件(application.properties或application.yaml),但其實springboot允許配置多個配置文件(application.properties或application.yaml),但是這并不意味著這些配置文件一定會替換默認生成的配置文件,它們是互補的存在。如果在某些場景下需要把配置文件單獨拿出來并且啟動的時候加載進去,那么外部的配置文件將是一個很好的選擇。
配置文件加載順序
需要注意的是配置文件加載順序加載順序在springboot 2.4.0前后是不一樣的。
springboot 2.4.0及其之前版本的配置文件加載順序
file:./config/
file:./config/*/
file:./
classpath:config/
classpath:
springboot 2.4.0之后版本的配置文件加載順序
file:./config/*/
file:./config/
file:./
classpath:config/
classpath:
區(qū)別在于springboot 2.4.0之后的版本將file:./config/*/的在順序調整為第一加載順序。
file是指當前jar包所在路徑。
classpath是指springboot resource文件夾下路徑。
驗證
前期準備
新建一個springboot項目
啟動類如下:
@SpringBootApplication
public class MqApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(MqApplication.class, args);
ConfigurableEnvironment environment = applicationContext.getEnvironment();
String property = environment.getProperty("spring.application.name");
System.out.println("current spring.application.name="+property);
}
}配置文件:
spring.application.name=classpath
server.port=8080
為了驗證 springboot 2.4.0之前和之后的版本加載順序的不一樣,會使用兩個版本對比。
對比版本:springboot 2.4.3 和 springboot 2.3.5.RELEASE
下面是不同路徑下配置不同端口和應用名以便驗證。
| 路徑 | 端口號 | application.name |
|---|---|---|
| file:./config/*/ | 8084 | file:./config/*/ |
| file:./config/ | 8083 | file:./config/ |
| file:./ | 8082 | file:./ |
| classpath:config/ | 8081 | classpath:config/ |
| classpath: | 8080 | classpath: |
驗證配置文件加載順序
根據(jù)上述表格,將配置文件分別復制到不同的路徑下創(chuàng)建配置文件并按表格修改spring.application.name和server.port屬性值。
啟動項目,下面是兩個版本的啟動信息:


從兩張圖中可以得出結論:
- springboot 2.4.0前后配置文件加載順序不一樣
- 高優(yōu)先級的會覆蓋掉低優(yōu)先級相同的屬性
驗證屬性互補
修改配置文件:
classpath:配置文件
刪除spring.application.name屬性,增加server.error.path屬性
server.port=8080
server.error.path=/test
file:./配置文件
新增server.servlet.context-path屬性
spring.application.name=file:.
server.port=8082
server.servlet.context-path=file_context
file:./config/*/配置文件
保持不變
server.port=8084
spring.application.name=file:./config/*/
修改啟動類main方法在控制臺打印server.error.path
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(MqApplication.class, args);
ConfigurableEnvironment environment = applicationContext.getEnvironment();
String property = environment.getProperty("spring.application.name");
System.out.println("current spring.application.name="+property);
String errorPath = environment.getProperty("server.error.path");
System.out.println("errorPath="+errorPath);
}
啟動項目

從上面截圖中可以發(fā)現(xiàn)三個配置文件中的所有屬性都被加載出來了,而且優(yōu)先級高的配置文件中的屬性會覆蓋優(yōu)先級低的配置文件中的屬性。
總結
springboot中可以配置多個配置文件,并且這些配置文件是可以共存的。當屬性相同時,優(yōu)先級高的配置文件會覆蓋優(yōu)先級低的配置文件中的屬性;當屬性不同時,最終的配置會取各個配置文件中屬性的并集。
到此這篇關于SpringBoot詳細講解多個配置文件的配置流程的文章就介紹到這了,更多相關SpringBoot配置文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot項目配置logback日志系統(tǒng)的實現(xiàn)
這篇文章主要介紹了springboot項目配置logback日志系統(tǒng)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04
Java如何將String轉換成json對象或json數(shù)組
這篇文章主要介紹了Java如何將String轉換成json對象或json數(shù)組,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
Java使用WeakHashMap實現(xiàn)緩存自動清理
在 Java 中,內存管理是一個重要的話題,尤其是在涉及到緩存的實現(xiàn)時,如果緩存項不再被使用,我們希望它們能被自動清理,而不必手動刪除,WeakHashMap 就是 Java 提供的一種用于緩存和內存管理的工具,本文將深入探討如何利用 WeakHashMap 來實現(xiàn)緩存自動清理2025-01-01
java網(wǎng)絡編程之識別示例 獲取主機網(wǎng)絡接口列表
一個客戶端想要發(fā)起一次通信,先決條件就是需要知道運行著服務器端程序的主機的IP地址是多少。然后我們才能夠通過這個地址向服務器發(fā)送信息。2014-01-01
Redisson分布式閉鎖RCountDownLatch的使用詳細講解
分布式鎖和我們java基礎中學習到的synchronized略有不同,synchronized中我們的鎖是個對象,當前系統(tǒng)部署在不同的服務實例上,單純使用synchronized或者lock已經(jīng)無法滿足對庫存一致性的判斷。本次主要講解基于rediss實現(xiàn)的分布式鎖2023-02-02

