Spring Boot 核心設定檔 bootstrap & application 詳解。

來源:互聯網
上載者:User

標籤:single   ref   .com   特性   weixin   應用程式   配置   關注   預設   

用過 Spring Boot 的都知道在 Spring Boot 中有以下兩種設定檔

  • bootstrap (.yml 或者 .properties)
  • application (.yml 或者 .properties)

為什麼會有這兩種設定檔呢?大家都清楚它們的區別和具體使用情境嗎?

bootstrap/ application 的區別

特意去翻了下 Spring Boot 的官方文檔,沒有找到關於這兩種檔案的具體定義,然後再翻了下 Spring Cloud 的官方文檔找到了它們的區別。

http://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_the_bootstrap_application_context

認真閱讀了下文檔,原文大概意思是這樣。

Spring Cloud 構建於 Spring Boot 之上,在 Spring Boot 中有兩種上下文,一種是 bootstrap, 另外一種是 application, bootstrap 是應用程式的父上下文,也就是說 bootstrap 載入優先於 applicaton。bootstrap 主要用於從額外的資源來載入配置資訊,還可以在本地外部設定檔中解密屬性。這兩個上下文共用一個環境,它是任何Spring應用程式的外部屬性的來源。bootstrap 裡面的屬性會優先載入,它們預設也不能被本地相同配置覆蓋。

因此,對比 application 設定檔,bootstrap 設定檔具有以下幾個特性。

  • boostrap 由父 ApplicationContext 載入,比 applicaton 優先載入
  • boostrap 裡面的屬性不能被覆蓋
bootstrap/ application 的應用情境

application 設定檔這個容易理解,主要用於 Spring Boot 項目的自動化配置。

bootstrap 設定檔有以下幾個應用情境。

  • 使用 Spring Cloud Config 配置中心時,這時需要在 bootstrap 設定檔中添加串連到配置中心的配置屬性來載入外部配置中心的配置資訊;
  • 一些固定的不能被覆蓋的屬性
  • 一些加密/解密的情境;

以下這個是一個國外網友問了一個 Spring Cloud 工程師得到的回答。

做過 Spring Cloud 微服務的朋友應該對 bootstrap 的應用十分清楚,我們也有 Spring Cloud 的實戰教程,在 Spring 專題中都能看到。
推薦:Spring Boot & Cloud 最強技術教程

掃描關注我們的公眾號,乾貨每天更新。

Spring Boot 核心設定檔 bootstrap & application 詳解。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.