springcloud如何配置文件加載順序
看了網(wǎng)上很多文章,都是清一色的說,優(yōu)先加載bootstrap配置文件,然后加載application配置文件,bootstrap配置文件不能被覆蓋。
今天實際驗證一下,配置文件真實的加載情況。
項目就是簡單的springboot項目,加入springcloud相關(guān)依賴。
項目結(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>
下面是各個配置文件
- 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
啟動類就不放了,直接看測試類
@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)上大部分文章,啟動的端口應(yīng)該是bootstrap配置當(dāng)中的,要么是8082,要么是8086. 下面看一下啟動日志:
有人可能會懷疑,是不是bootstrap配置文件沒有加載,現(xiàn)在看一下testAll接口,能否獲取到每個配置文件中配置的變量:
說明所有配置文件都是加載過的.
看一下test接口,可以看到,變量都是從application.properties文件中獲取的:
下面分析一下原因
spring官網(wǎng)的說明
我開始也是,光看到了這個描述忽略了前面的前提,那就是bootstrap context. 只有在bootstrap階段用到的屬性,在bootstrap配置中被加載了,才是優(yōu)先級最高。
繼續(xù)看spring官網(wǎng)的說明:
非bootstrap階段的屬性,bootstrap配置優(yōu)先級最低。
簡單的理解一下bootstrap階段,就是SpringConfig或者nacos用作配置中心的時候,讀取配置文件信息,就是bootstrap階段。
普通的springboot項目,讀取配置文件,是非bootstrap階段,因此bootstrap配置優(yōu)先級最低,優(yōu)先加載application配置文件。
下面再測試一下,將兩個application文件順序進(jìn)行調(diào)整,在resource目錄下,新建config目錄,將application.yml文件移到該文件夾中:
看一下日志:
可以看出來,端口已經(jīng)換成yml文件中的配置。
看一下接口:
下面試一下bootstrap優(yōu)先級問題,將bootstrap的一個配置移動到config目錄下
修改application.yml配置:
同時修改application.properties配置文件,去掉hello參數(shù):
再次啟動項目:
啟動的接口用的是application.properties文件中的,雖然bootstrap.properties加載的順序可能靠前,但是優(yōu)先級比application配置文件要低。
下面來看接口調(diào)用接口:
可以看到,hello參數(shù)是bootstrap.properties文件中的配置,而applicationName參數(shù),是application.yml配置文件中的,證明了所有的配置文件都會加載,以及加載的優(yōu)先級,確實在非bootstrap階段,bootstrap文件優(yōu)先級最低,其他的配置會按照application配置文件的加載順序,取優(yōu)先加載的配置文件中的配置。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
IntelliJ IDEA 創(chuàng)建 Java 項目及創(chuàng)建 Java 文件并運行的詳細(xì)步驟
這篇文章主要介紹了IntelliJ IDEA 創(chuàng)建 Java 項目及創(chuàng)建 Java 文件并運行的詳細(xì)步驟,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11SpringBoot整合Liquibase實現(xiàn)對數(shù)據(jù)庫管理和遷移
Liquibase是一個用于用于跟蹤、管理和應(yīng)用數(shù)據(jù)庫變化的開源工具,通過日志文件(changelog)的形式記錄數(shù)據(jù)庫的變更(changeset),然后執(zhí)行日志文件中的修改,將數(shù)據(jù)庫更新或回滾(rollback)到一致的狀態(tài),本文主要介紹SpringBoot與Liquibase的集成,需要的朋友可以參考下2024-11-11微服務(wù)通過Feign調(diào)用進(jìn)行密碼安全認(rèn)證操作
這篇文章主要介紹了微服務(wù)通過Feign調(diào)用進(jìn)行密碼安全認(rèn)證操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Java設(shè)計模式之共享模式/享元模式(Flyweight模式)介紹
這篇文章主要介紹了Java設(shè)計模式之共享模式/享元模式(Flyweight模式)介紹,本文講解了為什么使用共享模式/享元模式、如何使用共享模式/享元模式、Flyweight模式在XML等數(shù)據(jù)源中應(yīng)用等內(nèi)容,需要的朋友可以參考下2015-03-03