Spring 小技巧:設定

工程 | Josh Long | 2020 年 4 月 23 日 | ...

講者: Josh Long (@starbuxman)

嗨,Spring 迷們!歡迎來到另一個 Spring 小技巧!在這次的內容中,我們將研究一些相當基礎的東西,也是我希望早點談到的東西:設定。 不,我不是指函數式設定或 Java 設定之類的,我說的是那些告知您的程式碼如何執行的字串值。您放在 application.properties 中的東西。那個設定。

Spring 中的所有設定都源自 Spring 的 Environment 抽象概念。Environment 就像一個字典 - 一個具有鍵和值的對應。 Environment 只是我們可以用來詢問關於 Environment 的介面。這個抽象概念存在於 Spring Framework 中,並且在十多年前的 Spring 3 中引入。在那之前,有一種專注的機制允許設定的整合,稱為屬性佔位符解析。這種環境機制和圍繞該介面的一系列類別,已經完全取代了舊的支援。如果您發現還有部落格在使用這些類型,我建議您轉向更新、更好的領域?:)

讓我們開始吧。前往 Spring Initializr 並生成一個新專案,並確保選擇 Spring Cloud VaultLombokSpring Cloud Config Client。我將我的專案命名為 configuration。繼續並點擊 Generate 應用程式。在您最喜歡的 IDE 中打開專案。如果您想跟著操作,請務必禁用 Spring Cloud Vault 和 Spring Cloud Config Client 依賴項。我們現在不需要它們。

對於大多數 Spring Boot 開發人員來說,第一步是使用 application.properties。當您在那裡生成一個新專案時,Spring Initializr 甚至會在 src/main/resources/application.properties 資料夾中放入一個空的 application.properties!超級方便。您確實在 Spring Initializr 上建立您的專案,不是嗎?您可以使用 application.properties 或 application.yml。我並不是特別喜歡 .yml 檔案,但如果那更符合您的口味,您可以使用它。

Spring Boot 會在每次啟動時自動載入 application.properties。您可以通過環境在您的 Java 程式碼中取消引用屬性檔案中的值。將一個屬性放入 application.properties 檔案中,像這樣。

message-from-application-properties=Hello from application.properties

現在,讓我們編輯程式碼以讀取該值。

package com.example.configuration;

import lombok.extern.log4j.Log4j2;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

@Log4j2
@SpringBootApplication
public class ConfigurationApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigurationApplication.class, args);
    }

    @Bean
    ApplicationRunner applicationRunner(Environment environment) {
        return args -> {
            log.info("message from application.properties " + environment.getProperty("message-from-application-properties"));
        };
    }
}

執行此程式,您將在日誌的輸出中看到組態屬性檔案中的值。如果您想更改 Spring Boot 預設讀取的檔案,您也可以這樣做。但這是一個雞生蛋、蛋生雞的問題 - 您需要指定 Spring Boot 將用來確定從哪裡載入所有屬性的屬性。因此,您需要在 application.properties 檔案之外指定它。您可以使用程式參數或環境變數來填寫 spring.config.name 屬性。

export SPRING_CONFIG_NAME=foo

現在使用該環境變數重新運行應用程式,它將失敗,因為它會嘗試載入 foo.properties,而不是 application.properties

順便說一句,您也可以運行配置位於應用程式外部、與 jar 相鄰的應用程式,像這樣。如果您像這樣運行應用程式,則外部 application.properties 中的值將覆蓋 .jar 內的值。

.
├── application.properties
└── configuration-0.0.1-SNAPSHOT.jar

0 directories, 2 files

Spring Boot 也知道 Spring 配置文件。配置文件是一種機制,允許您標記物件和屬性檔案,以便它們可以在運行時有選擇地啟用或停用。如果您想要具有特定於環境的組態,這非常有用。您可以將 Spring bean 或組態檔案標記為屬於特定配置文件,當該配置文件被啟用時,Spring 會自動為您載入它。

配置文件名稱基本上是任意的。有些配置文件是神奇的 - Spring 以特定方式尊重它們。其中最有趣的是 default,它在沒有其他配置文件處於活動狀態時被啟用。但通常,這些名稱由您決定。我發現將我的配置文件映射到不同的環境非常有用:devqastagingprod 等。

假設有一個名為 dev 的配置文件。Spring Boot 將自動載入 application-dev.properties。除了 application.properties 之外,它還會載入它。如果兩個檔案中的值之間有任何衝突,則更具體的檔案 - 具有配置文件的檔案 - 獲勝。您可以有一個預設值,在沒有特定配置檔案的情況下應用,然後在配置檔案中提供具體資訊。

您可以用幾種不同的方式啟用給定的配置文件,但最簡單的方法是在命令行上指定它。或者,您可以在 IDE 的運行配置對話框中打開它。IntelliJ 和 Spring Tool Suite 都提供了一個指定在運行應用程式時使用的配置文件的位置。您還可以設定環境變數 SPRING_PROFILES_ACTIVE,或者在命令行上指定參數 --spring.profiles.active。兩者都接受逗號分隔的配置文件列表 - 您可以一次啟用多個配置文件。

讓我們嘗試一下。建立一個名為 application-dev.properties 的檔案。將以下值放入其中。

message-from-application-properties=Hello from dev application.properties

此屬性的鍵與 application.properties 中的鍵相同。此處的 Java 程式碼與我們之前的程式碼相同。只需確保在啟動 Spring 應用程式之前指定配置文件即可。您可以使用環境變數、屬性等。您甚至可以在 main() 方法中建立 SpringApplication 時以程式方式定義它。

package com.example.configuration.profiles;

import lombok.extern.log4j.Log4j2;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

@Log4j2
@SpringBootApplication
public class ConfigurationApplication {

    public static void main(String[] args) {
        // this works
        // export SPRING_PROFILES_ACTIVE=dev  
        // System.setProperty("spring.profiles.active", "dev"); // so does this
        new SpringApplicationBuilder()
            .profiles("dev") // and so does this
            .sources(ConfigurationApplication.class)
            .run(args);
    }

    @Bean
    ApplicationRunner applicationRunner(Environment environment) {
        return args -> {
            log.info("message from application.properties " + environment.getProperty("message-from-application-properties"));
        };
    }
}

運行應用程式,您將在輸出中看到特殊的消息。

到目前為止,我們一直在使用環境來注入配置。您也可以使用 @Value 註解將該值作為參數注入。您可能已經知道了。但是您是否知道您還可以指定在沒有其他匹配值時要返回的預設值?您可能有很多理由想要這樣做。您可以使用它來提供後備值,並在有人笨手笨腳地拼寫屬性時使其更透明。這也很有用,因為如果有人不知道他們需要啟用配置文件或類似的東西,您會得到一個可能很有用的值。

package com.example.configuration.value;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@Log4j2
@SpringBootApplication
public class ConfigurationApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigurationApplication.class, args);
    }

    @Bean
    ApplicationRunner applicationRunner(
        @Value("${message-from-application-properties:OOPS!}") String valueDoesExist,
        @Value("${mesage-from-application-properties:OOPS!}") String valueDoesNotExist) {
        return args -> {
            log.info("message from application.properties " + valueDoesExist);
            log.info("missing message from application.properties " + valueDoesNotExist);
        };
    }
}

方便,對吧?另外,請注意,您提供的預設字串反過來也可以內插一些其他屬性。因此,您可以執行類似於此的操作,假設 default-error-message 之類的鍵確實存在於您的應用程式配置中的某個位置

${message-from-application-properties:${default-error-message:YIKES!}}

如果第一個屬性存在,則將評估該屬性,然後評估第二個屬性,然後評估字串 YIKES!,最後。

之前,我們研究了如何使用環境變數或程式參數指定配置文件。這種機制 - 使用環境變數或程式參數配置 Spring Boot - 是一種通用機制。您可以將它用於任何任意鍵,Spring Boot 會為您標準化配置。您可以放入 application.properties 中的任何鍵都可以通過這種方式在外部指定。讓我們看一些例子。假設您要指定資料來源連線的 URL。您可以在 application.properties 中硬編碼該值,但這不是很安全。最好創建一個僅存在於生產環境中的環境變數。這樣,開發人員就無法訪問生產資料庫的密鑰等等。

讓我們嘗試一下。這是該範例的 Java 程式碼。


package com.example.configuration.envvars;

import lombok.extern.log4j.Log4j2;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

@Log4j2
@SpringBootApplication
public class ConfigurationApplication {

    public static void main(String[] args) {
        // simulate program arguments
        String[] actualArgs = new String[]{"spring.datasource.url=jdbc:postgres://127.0.0.1/some-prod-db"};
        SpringApplication.run(ConfigurationApplication.class, actualArgs);
    }

    @Bean
    ApplicationRunner applicationRunner(Environment environment) {
        return args -> {
            log.info("our database URL connection will be " + environment.getProperty("spring.datasource.url"));
        };
    }
}

在運行它之前,請確保在用於運行應用程式的 shell 中匯出環境變數,或者指定程式參數。我通過攔截傳遞到此處的 Spring Boot 應用程式的 public static void main(String [] args) 來模擬後者 - 程式參數。您也可以像這樣指定環境變數

export SPRING_DATASOURCE_URL=some-arbitrary-value
mvn -DskipTests=true spring-boot:run 

多次運行程式,嘗試不同的方法,您將在輸出中看到這些值。應用程式中沒有自動配置來連線到資料庫,因此我們將此屬性作為一個範例。URL 不必是有效的 URL(至少在您將 Spring 的 JDBC 支援和 JDBC 驅動程式新增到類別路徑之前)。

Spring Boot 在其值的來源方面非常靈活。它不在乎您是否執行 SPRING_DATASOURCE_URLspring.datasource.url 等。Spring Boot 將此稱為寬鬆繫結。它允許您以最適合不同環境的方式執行操作,同時仍然適用於 Spring Boot。

這種想法 - 從環境中外部化應用程式的配置 - 並不新鮮。它已在 12-factor manifesto 中得到充分理解和描述。12-factor manifesto 說特定於環境的配置應該存在於該環境中,而不是在程式碼本身中。這是因為我們希望所有環境都有一個建置。應該更改的東西應該是外部的。到目前為止,我們已經看到 Spring Boot 可以從命令行參數(程式參數)和環境變數中提取配置。它甚至可以從 JOpt 中讀取配置。如果您碰巧在具有其中一個的應用程式伺服器中運行,它甚至可以來自 JNDI 上下文!

Spring Boots 提取任何環境變數的能力在此處非常有用。它也比使用程式參數更安全,因為程式參數會顯示在作業系統工具的輸出中。環境變數更適合。

到目前為止,我們已經看到 Spring Boot 可以從許多不同的地方拉取配置。它了解 profiles,也了解 .yml.properties。它相當有彈性!但是,如果它不知道如何執行您想要執行的操作呢?您可以使用自定義的 PropertySource<T> 輕鬆地教它新的技巧。例如,如果您希望將應用程式與儲存在外部資料庫或目錄,或者 Spring Boot 無法自動識別的其他地方的配置整合,您可能會想執行類似的操作。


package com.example.configuration.propertysource;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.PropertySource;

@Log4j2
@SpringBootApplication
public class ConfigurationApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder()
            .sources(ConfigurationApplication.class)
            .initializers(context -> context
                .getEnvironment()
                .getPropertySources()
                .addLast(new BootifulPropertySource())
            )
            .run(args);
    }

    @Bean
    ApplicationRunner applicationRunner(@Value("${bootiful-message}") String bootifulMessage) {
        return args -> {
            log.info("message from custom PropertySource: " + bootifulMessage);
        };
    }
}

class BootifulPropertySource extends PropertySource<String> {

    BootifulPropertySource() {
        super("bootiful");
    }

    @Override
    public Object getProperty(String name) {

        if (name.equalsIgnoreCase("bootiful-message")) {
            return "Hello from " + BootifulPropertySource.class.getSimpleName() + "!";
        }

        return null;
    }
}


上面的範例是註冊 PropertySource 的最安全方式,可以確保所有需要它的東西都能夠及早找到它。您也可以在 Spring 開始將物件連接在一起,並且您有權存取已配置的物件時,在執行時期執行此操作,但我不確定這是否在每種情況下都有效。以下是它的可能外觀。

package com.example.configuration.propertysource;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;

@Log4j2
@SpringBootApplication
public class ConfigurationApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigurationApplication.class, args);
    }

    @Bean
    ApplicationRunner applicationRunner(@Value("${bootiful-message}") String bootifulMessage) {
        return args -> {
            log.info("message from custom PropertySource: " + bootifulMessage);
        };
    }

    @Autowired
    void contributeToTheEnvironment(ConfigurableEnvironment environment) {
        environment.getPropertySources().addLast(new BootifulPropertySource());
    }
}

class BootifulPropertySource extends PropertySource<String> {

    BootifulPropertySource() {
        super("bootiful");
    }

    @Override
    public Object getProperty(String name) {

        if (name.equalsIgnoreCase("bootiful-message")) {
            return "Hello from " + BootifulPropertySource.class.getSimpleName() + "!";
        }

        return null;
    }
}

到目前為止,我們幾乎完全著重於如何從其他地方取得屬性值。不過,我們還沒有討論這些字串一旦進入我們的記憶體並可用於應用程式時會發生什麼事。大多數時候,它們只是字串,我們可以按原樣使用它們。但是,有時將它們轉換為其他類型的值(例如 int、Date、double 等)會很有用。這種將字串轉換為其他東西的工作可以成為另一個 Spring Tips 影片的主題,也許我很快就會製作一個。可以說,其中有很多相互關聯的部分 - ConversionServiceConverter<T>s、Spring Boot 的 Binders 等等。對於常見情況,這一切都會正常運作。例如,您可以指定一個屬性 server.port = 8080,然後將它作為 int 注入到您的應用程式中。

@Value("${server.port}") int port

如果能自動將這些值綁定到一個物件,可能會很有幫助。這正是 Spring Boot 的 ConfigutationProperties 為您所做的事情。讓我們看看它的實際運作。

假設您有一個包含以下屬性的 application.properties 檔案

bootiful.message = Hello from a @ConfiguratinoProperties 

然後您可以執行應用程式,並看到配置值已綁定到物件上。

package com.example.configuration.cp;

import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

@Log4j2
@SpringBootApplication
@EnableConfigurationProperties(BootifulProperties.class)
public class ConfigurationApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigurationApplication.class, args);
    }
    
    @Bean
    ApplicationRunner applicationRunner(BootifulProperties bootifulProperties) {
        return args -> {
            log.info("message from @ConfigurationProperties " + bootifulProperties.getMessage());
        };
    }
 
}

@Data
@RequiredArgsConstructor
@ConstructorBinding
@ConfigurationProperties("bootiful")
class BootifulProperties {
    private final String message;
}

BootifulProperties 物件上的 @Data@RequiredArgsConstructor 註解來自 Lombok。 @Data 為 final 欄位合成 getter,為非 final 欄位合成 getter 和 setter。 @RequiredArgsConstructor 為類別中的所有 final 欄位合成建構子。結果是一個一旦通過建構子初始化建構完成就不可變的物件。預設情況下,Spring Boot 的 ConfigurationProperties 機制不了解不可變物件;您需要使用 @ConstructorBinding 註解(這是 Spring Boot 中一個相當新的新增功能)才能使其在此處執行正確的操作。這在其他程式語言(如 Kotlin (data class ...) 和 Scala (case class ...))中甚至更有用,這些語言具有用於建立不可變物件的語法糖。

我們已經看到 Spring 可以載入應用程式 .jar 旁邊的配置,並且可以從環境變數和程式參數載入配置。將資訊導入 Spring Boot 應用程式並不難,但它有點零碎。很難對環境變數進行版本控制或保護程式參數。

為了解決其中的一些問題,Spring Cloud 團隊建構了 Spring Cloud Config Server。 Spring Cloud Config Server 是一個 HTTP API,它位於後端儲存引擎的前面。儲存是可插拔的,最常見的是 Git 儲存庫,但也支援其他儲存方式。這些包括 Subversion、本機檔案系統,甚至 MongoDB

我們將設定一個新的 Spring Cloud Config Server。前往 Spring Initializr 並選擇 Config Server,然後點擊 Generate。在您最喜歡的 IDE 中開啟它。

我們需要做兩件事才能使它運作:首先,我們必須使用註解,然後提供配置值,將其指向包含我們配置檔案的 Git 儲存庫。以下是 application.properties

spring.cloud.config.server.git.uri=https://github.com/joshlong/greetings-config-repository.git
server.port=8888

這是您的主要類別應該是什麼樣子。

package com.example.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

執行應用程式 - mvn spring-boot:run 或直接在您最喜歡的 IDE 中執行應用程式。它現在可用了。它將充當 Github 儲存庫中 Git 配置的代理。然後,其他用戶端可以使用 Spring Cloud Config Client 從 Spring Cloud Config Server 拉取其配置,而 Spring Cloud Config Server 又將從 Git 儲存庫拉取配置。注意:為了便於演示,我將其設定為盡可能不安全,但您可以並且應該保護鏈中的兩個連結 - 從 config 用戶端到 config 伺服器,以及從 config 伺服器到 git 儲存庫。 Spring Cloud Config Server、Spring Cloud Config Client 和 Github 都能很好且安全地協同工作。

現在,回到我們配置應用程式的建置,並確保取消註解 Spring Cloud Config Client 依賴。要啟動 Spring Cloud Config Server,它需要一些 - 您猜對了! - 配置。一個經典的雞生蛋問題。此配置需要提前評估,在其他配置之前。您可以將此配置放在名為 bootstrap.properties 的檔案中。

您需要識別您的應用程式並給它一個名稱,以便當它連接到 Spring Cloud Config Server 時,它將知道要提供哪個配置。我們在這裡指定的名稱將與 Git 儲存庫中的屬性檔案相符。以下是您應該放在檔案中的內容。

spring.cloud.config.uri=https://127.0.0.1:8888
spring.application.name=bootiful

現在,我們可以讀取 git 儲存庫中 bootiful.properties 檔案中的任何值,其內容為

message-from-config-server = Hello, Spring Cloud Config Server

我們可以像這樣拉取該配置檔案

package com.example.configuration.configclient;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@Log4j2
@SpringBootApplication
public class ConfigurationApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigurationApplication.class, args);
    }

    @Bean
    ApplicationRunner applicationRunner(@Value("${message-from-config-server}") String configServer) {
        return args -> {
            log.info("message from the Spring Cloud Config Server: " + configServer);
        };
    }
}

您應該在輸出中看到該值。不錯! Spring Cloud Config Server 為我們做了很多很酷的事情。它可以為我們加密值。它可以幫助版本化屬性。我最喜歡的一件事是您可以獨立於程式碼庫的變更來變更配置。您可以將其與 Spring Cloud @RefreshScope 結合使用,以在應用程式啟動執行後動態重新配置應用程式。(我應該製作一個關於 refresh scope 及其眾多用途的影片...)Spring Cloud Config Server 是最受歡迎的 Spring Cloud 模組之一,這是理所當然的 - 它可以與單體應用程式和微服務一起使用。

如果您適當配置,Spring Cloud Config Server 可以加密屬性檔案中的值。它可以運作。很多人也使用 Hashicorp 出色的 Vault 產品,這是一款功能更完善的安全產品。 Vault 可以使用 UI、CLI 或 HTTP API 來保護、儲存和嚴格控制對令牌、密碼、憑證、用於保護機密的加密金鑰和其他敏感資料的存取。您也可以使用 Spring Cloud Vault 專案輕鬆地將其用作屬性來源。從建置中取消註解 Sring Cloud Vault 依賴,讓我們看看如何設定 Hashicorp Vault。

下載最新版本,然後執行以下命令。我假設是一個 Linux 或類 Unix 環境。轉換為 Windows 應該相當簡單。我不會嘗試解釋關於 Vault 的所有內容;我建議您參考 Hashicorp Vault 的優秀入門指南。以下是我所知道的,設定和運作所有這些的最不安全,但最快速的方法。首先,執行 Vault 伺服器。我在此提供一個根令牌,但您通常會使用 Vault 在啟動時提供的令牌。

export VAULT_ADDR="https://127.0.0.1:8200"
export VAULT_SKIP_VERIFY=true
export VAULT_TOKEN=00000000-0000-0000-0000-000000000000
vault server --dev --dev-root-token-id="00000000-0000-0000-0000-000000000000"

啟動後,在另一個 shell 中,將一些值安裝到 Vault 伺服器中,如下所示。

export VAULT_ADDR="https://127.0.0.1:8200"
export VAULT_SKIP_VERIFY=true
export VAULT_TOKEN=00000000-0000-0000-0000-000000000000
vault kv put secret/bootiful message-from-vault-server="Hello Spring Cloud Vault"

這會將金鑰 message-from-vault-server 和值 Hello Spring Cloud Vault 放入 Vault 服務中。現在,讓我們變更我們的應用程式以連接到該 Vault 實例,以讀取安全值。我們需要一個 bootstrap.properties,就像使用 Spring Cloud Config Client 一樣。

spring.application.name=bootiful
spring.cloud.vault.token=${VAULT_TOKEN}
spring.cloud.vault.scheme=http

然後,您可以像使用任何其他配置值一樣使用該屬性。

package com.example.configuration.vault;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@Log4j2
@SpringBootApplication
public class ConfigurationApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigurationApplication.class, args);
    }

    @Bean
    ApplicationRunner applicationRunner(@Value("${message-from-vault-server:}") String valueFromVaultServer) {
        return args -> {
            log.info("message from the Spring Cloud Vault Server : " + valueFromVaultServer);
        };
    }
}

現在,在您執行此操作之前,請確保也配置了我們在與 vault CLI 互動時使用的相同三個環境變數:VAULT_TOKENVAULT_SKIP_VERIFYVAULT_ADDR。然後執行它,您應該在控制台上看到您寫入 Hashicorp Vault 的值。

下一步

希望您已經了解了一些關於 Spring 中色彩繽紛且引人入勝的配置世界。有了這些資訊,您現在可以更好地使用支援屬性解析的其他專案。掌握了如何運作的知識,您就可以準備好整合來自不同 Spring 整合的配置,這些整合非常多!您可以使用 Spring Cloud Netflix 的 Archaius 整合,或 Configmaps 與 Spring Cloud Kubernetes 的整合,或 Spring Cloud GCP 的 Google Runtime Configuration API 整合,或 Spring Cloud Azure 的 Microsoft Azure Key Vault 整合等等。

我只提到了一些產品,但清單是否詳盡無關緊要,如果整合正確,它們的用途將是相同的:雲端就是極限!

取得 Spring 電子報

透過 Spring 電子報保持聯繫

訂閱

領先一步

VMware 提供培訓和認證,以加速您的進展。

了解更多

取得支援

Tanzu Spring 在一個簡單的訂閱中提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位檔案。

了解更多

即將舉辦的活動

查看 Spring 社群中所有即將舉辦的活動。

查看全部