簡體   English   中英

從 application.yml spring boot 中的外部文件讀取值

[英]Read values from external file in application.yml spring boot

明文值在 /tmp/pass 文件中可用,需要在 application.yml spring 啟動組件中讀取

示例 Spring.datasource.password:文件(/tmp/pass)

Spring.datasource.password:文件:/tmp/pass

兩者都不起作用,還有其他方法可以從 application.yml 中的外部文件讀取內容嗎?

有一種方法可以做到這一點,但它可能不是最好的方法。 假設您有以下 application.yml:

spring:
    some-config:
        password: FILE(xxx)
        username: FILE(yyy)
        other-properties: FILE(zzz)

創建以下配置文件:

@Configuration
@ConfigurationProperties("spring.some-config")
public class SomeConfigSettings {
    private String password;
    private String username;
    private String otherProperties;

    private String getPassword() {
        return this.password;
    }
    private void setPassword(String password) {
        this.password = FileUtil.read(password)
    }

    // Provide getter/setters for remaining fields
    .......
}

在應用程序啟動期間,spring 將使用 application.yml 中提供的值調用 setter 方法。 您只需要實現您的 FileUtil.read 方法,以便它使用您的格式 FILE(/file/path)。

然后您可以訪問 bean 中的 spring.some-config 屬性:

@Autowire
private SomeConfigSettings someConfigSettings;
....

public void someMethod() {
    someConfigSettings.getPassword();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM