亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springcloud如何配置文件加載順序

 更新時間:2024年07月15日 09:00:34   作者:一個胖子IT男  
這篇文章主要介紹了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ì)步驟

    這篇文章主要介紹了IntelliJ IDEA 創(chuàng)建 Java 項目及創(chuàng)建 Java 文件并運行的詳細(xì)步驟,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • SpringBoot整合Liquibase實現(xiàn)對數(shù)據(jù)庫管理和遷移

    SpringBoot整合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)證操作

    這篇文章主要介紹了微服務(wù)通過Feign調(diào)用進(jìn)行密碼安全認(rèn)證操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java設(shè)計模式之共享模式/享元模式(Flyweight模式)介紹

    Java設(shè)計模式之共享模式/享元模式(Flyweight模式)介紹

    這篇文章主要介紹了Java設(shè)計模式之共享模式/享元模式(Flyweight模式)介紹,本文講解了為什么使用共享模式/享元模式、如何使用共享模式/享元模式、Flyweight模式在XML等數(shù)據(jù)源中應(yīng)用等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • 淺析JVM垃圾回收的過程

    淺析JVM垃圾回收的過程

    這篇文章主要介紹了JVM垃圾回收的過程,幫助大家更好的理解和學(xué)習(xí)Java中的垃圾回收機制,感興趣的朋友可以了解下
    2020-09-09
  • JAVA中的字符串常量池使用操作代碼

    JAVA中的字符串常量池使用操作代碼

    Java中的字符串常量池是Java堆中的一塊特殊存儲區(qū)域,用于存儲字符串。它的實現(xiàn)是為了提高字符串操作的性能并節(jié)省內(nèi)存,這篇文章主要介紹了JAVA中的字符串常量池,需要的朋友可以參考下
    2022-12-12
  • java程序運行時內(nèi)存分配詳解

    java程序運行時內(nèi)存分配詳解

    這篇文章主要介紹了java程序運行時內(nèi)存分配詳解 ,需要的朋友可以參考下
    2016-07-07
  • Spring?Feign超時設(shè)置深入了解

    Spring?Feign超時設(shè)置深入了解

    Spring?Cloud中Feign客戶端是默認(rèn)開啟支持Ribbon的,最重要的兩個超時就是連接超時ConnectTimeout和讀超時ReadTimeout,在默認(rèn)情況下,也就是沒有任何配置下,F(xiàn)eign的超時時間會被Ribbon覆蓋,兩個超時時間都是1秒
    2023-03-03
  • Jar包如何導(dǎo)入本地maven倉庫

    Jar包如何導(dǎo)入本地maven倉庫

    將本地jar包導(dǎo)入本地maven倉庫,可以通過maven命令-Dfile、-DgroupId、-DartifactId、-Dversion、-Dpackaging指定jar包的詳細(xì)信息,然后執(zhí)行命令即可
    2024-11-11
  • Java中File類中常用方法詳解

    Java中File類中常用方法詳解

    這篇文章主要為大家詳細(xì)介紹了File類中常用方法的程序演示,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08

最新評論