繁体   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