springcloud如何配置文件加載順序
看了網(wǎng)上很多文章,都是清一色的說,優(yōu)先加載bootstrap配置文件,然后加載application配置文件,bootstrap配置文件不能被覆蓋。
今天實(shí)際驗(yàn)證一下,配置文件真實(shí)的加載情況。
項(xiàng)目就是簡單的springboot項(xiàng)目,加入springcloud相關(guān)依賴。
項(xiàng)目結(jié)構(gòu)
如下:

pom文件
如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Cloud Alibaba 依賴 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
下面是各個(gè)配置文件
- application.properties
server.port=8081 hello1=hello1aaa hello= application.properties spring.application.name= test.application.properties
- application.yml
server:
port: 8083
hello2: hello2bbb
hello: application.yml
spring:
application:
name: test.application.yml
- bootstrap.properties
server.port = 8082 hello3=hello bp hello=bootstrap.properties spring.application.name=test.bootstrap.properties
- bootstrap.yml
server:
port: 8086
hello4: hello by
hello: bootstrap.yml
spring:
application:
name: test.bootstrap.yml
啟動(dòng)類就不放了,直接看測試類
@RestController
public class TestController {
@Value("${hello}")
private String hello;
@Value("${hello1}")
private String hello1;
@Value("${hello2}")
private String hello2;
@Value("${hello3}")
private String hello3;
@Value("${hello4}")
private String hello4;
@Value("${spring.application.name}")
private String applicationName;
@GetMapping("/testAll")
public String testAll(){
return hello1 + " === " + hello2
+ " === " + hello3 + " === " + hello4;
}
@GetMapping("/test")
public String test(){
return hello + " ====== " + applicationName;
}
}
按照網(wǎng)上大部分文章,啟動(dòng)的端口應(yīng)該是bootstrap配置當(dāng)中的,要么是8082,要么是8086. 下面看一下啟動(dòng)日志:

有人可能會(huì)懷疑,是不是bootstrap配置文件沒有加載,現(xiàn)在看一下testAll接口,能否獲取到每個(gè)配置文件中配置的變量:

說明所有配置文件都是加載過的.
看一下test接口,可以看到,變量都是從application.properties文件中獲取的:

下面分析一下原因
spring官網(wǎng)的說明

我開始也是,光看到了這個(gè)描述忽略了前面的前提,那就是bootstrap context. 只有在bootstrap階段用到的屬性,在bootstrap配置中被加載了,才是優(yōu)先級(jí)最高。
繼續(xù)看spring官網(wǎng)的說明:

非bootstrap階段的屬性,bootstrap配置優(yōu)先級(jí)最低。
簡單的理解一下bootstrap階段,就是SpringConfig或者nacos用作配置中心的時(shí)候,讀取配置文件信息,就是bootstrap階段。
普通的springboot項(xiàng)目,讀取配置文件,是非bootstrap階段,因此bootstrap配置優(yōu)先級(jí)最低,優(yōu)先加載application配置文件。
下面再測試一下,將兩個(gè)application文件順序進(jìn)行調(diào)整,在resource目錄下,新建config目錄,將application.yml文件移到該文件夾中:

看一下日志:

可以看出來,端口已經(jīng)換成yml文件中的配置。
看一下接口:

下面試一下bootstrap優(yōu)先級(jí)問題,將bootstrap的一個(gè)配置移動(dòng)到config目錄下

修改application.yml配置:

同時(shí)修改application.properties配置文件,去掉hello參數(shù):

再次啟動(dòng)項(xiàng)目:


啟動(dòng)的接口用的是application.properties文件中的,雖然bootstrap.properties加載的順序可能靠前,但是優(yōu)先級(jí)比application配置文件要低。
下面來看接口調(diào)用接口:

可以看到,hello參數(shù)是bootstrap.properties文件中的配置,而applicationName參數(shù),是application.yml配置文件中的,證明了所有的配置文件都會(huì)加載,以及加載的優(yōu)先級(jí),確實(shí)在非bootstrap階段,bootstrap文件優(yōu)先級(jí)最低,其他的配置會(huì)按照application配置文件的加載順序,取優(yōu)先加載的配置文件中的配置。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA自定義setter和getter格式的設(shè)置方法
這篇文章主要介紹了IDEA自定義setter和getter格式的設(shè)置方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-12-12
java開發(fā)RocketMQ生產(chǎn)者高可用示例詳解
這篇文章主要為大家介紹了java開發(fā)RocketMQ生產(chǎn)者高可用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Idea中如何查看SpringSecurity各Filter信息
這篇文章主要介紹了Idea中如何查看SpringSecurity各Filter信息,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Java中調(diào)用數(shù)據(jù)庫存儲(chǔ)過程的示例代碼
本文介紹Java通過JDBC調(diào)用數(shù)據(jù)庫存儲(chǔ)過程的方法,涵蓋參數(shù)類型、執(zhí)行步驟及數(shù)據(jù)庫差異,需注意異常處理與資源管理,以優(yōu)化性能并實(shí)現(xiàn)復(fù)雜業(yè)務(wù)邏輯,感興趣的朋友一起看看吧2025-06-06
mybatis中查詢結(jié)果為空時(shí)不同返回類型對應(yīng)返回值問題
這篇文章主要介紹了mybatis中查詢結(jié)果為空時(shí)不同返回類型對應(yīng)返回值問題,本文分幾種方法給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-10-10

