【问题标题】:Read values from external file in application.yml spring boot从 application.yml spring boot 中的外部文件读取值
【发布时间】:2021-06-03 17:56:37
【问题描述】:

明文值在/tmp/pass文件中,需要在application.yml spring boot组件中读取

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

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

两者都不起作用,有没有其他方法可以从 application.yml 中的外部文件读取内容?

【问题讨论】:

标签: spring spring-boot aws-ssm


【解决方案1】:

有一种方法可以做到这一点,但这可能不是最好的方法。假设你有以下 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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2020-01-22
    • 2015-03-18
    • 2020-08-05
    • 1970-01-01
    相关资源
    最近更新 更多