Spring Cloud - 自訂引導屬性源

來源:互聯網
上載者:User

標籤:springcloud   spring cloud   springcloud微服務   微服務雲架構   

引導過程添加的外部配置的預設屬性源是Config Server,但您可以通過將PropertySourceLocator類型的bean添加到引導上下文(通過spring.factories)添加其他源。您可以使用此方法從其他伺服器或資料庫中插入其他屬性。

作為一個例子,請考慮以下微不足道的自訂定位器:

@Configurationpublic class CustomPropertySourceLocator implements PropertySourceLocator {    @Override    public PropertySource<?> locate(Environment environment) {        return new MapPropertySource("customProperty",                Collections.<String, Object>singletonMap("property.from.sample.custom.source", "worked as intended"));    }}

傳入的Environment是要建立的ApplicationContext的Environment,即為我們提供額外的屬性來源的。它將已經具有正常的Spring Boot提供的資源來源,因此您可以使用它們來定位特定於此Environment的屬性源(例如通過將其綁定在spring.application.name上,如在預設情況下所做的那樣Config Server屬性源定位器)。

如果你在這個類中建立一個jar,然後添加一個META-INF/spring.factories包含:

org.springframework.cloud.bootstrap.BootstrapConfiguration=sample.custom.CustomPropertySourceLocator

那麼“customProperty”PropertySource將顯示在其類路徑中包含該jar的任何應用程式中。

更多資料和來源查看

Spring Cloud - 自訂引導屬性源

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.