springboot項目中的bootstrap.yml配置不生效(沒有自動提示也沒有變綠葉)


1、背景與現象:

(1)新創建一個 springboot項目,添加了 bootstrap.yml 文件,發現文件並沒有如預期變成綠色葉子,編寫的時候也沒有自動提示。

(2)啟動的時候,發現端口是8080,並沒有如配置上寫的端口進行啟動——由此發現配置並沒有生效,所以是為什么呢。。。

 

原因之一:

2、問題定位

為什么不生效呢,通過資料查找,原來:

(1)單純的 springboot 項目只會識別 application.* 配置文件, bootstrap.yml 它不認識啊,所以並沒有被識別為配置文件,所以也不會變成綠葉,也沒有自動提示了。——對應方法1 : 把配置文件改為 application.*

 

(2)如果是 springcloud 項目,就能夠識別 bootstrap.yml | bootstrap.properties,相同配置以 bootstrap.yml 為准。如果還有 application文件, bootstrap 優先於 application 文件(以 application 為准)

 

(3)那我剛才建的項目,它是一個 springboot項目,還不是一個springcloud項目,兩者要怎樣去切換呢?

添加一個依賴,就把 springboot 變成 springcloud 了

 <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-bootstrap</artifactId>
 </dependency>

 

 

原因之二:

 一個最簡單的 springboot 項目,

在pom.xml 引入的依賴是:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.5.6</version>
        </dependency>

在 resources 下面的 application.yml 沒有變綠葉

可能只是 idea 沒有安裝對應的插件,
去plugins 安裝一下插件可能就好了。
image

安裝了這個,它就好了。。。。




原因之三

resources文件夾 要標記一下。如果這個 resources 的文件夾是普通的文件夾(沒有標識),好像也是不會變綠葉的。
ctrl + shift + s 或者 command + ; 打開這個配置頁面,設置一下。
image

 

 

 

 

 

優先級

3、bootstrap.yml, bootstrap.properties, application.yml, application.properties 優先級哪個高,以哪個為准?

加載順序(優先級): bootstrap > application > 配置中心

bootstrap.yml > bootstrap.properties > application.yml > application.properties

后者覆蓋前者

 

所以,最終,是以配置中心為准?emm

!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM