如何将属性从application.yaml注入到config bean?

时间:2019-03-28 11:02:27

标签: java spring spring-boot

我有一个主要的配置文件,即:

@Data
@Component
@ConfigurationProperties("main.config")
public class AerospikeCacheConfig {
    private String host;
    private int port;
    private int maxConnectionsPerEventLoop;
    private SubConfig1 subConfig1;
    private SubConfig2 subConfig2;
}

SubConfig1SubConfig2有一个共同的属性,称为namespace,它是第一个文件代码:

@Getter
public class SubConfig1 extends Config {

    private final long refreshCounterServiceInterval;

    public SubConfig1(String namespace, int minRemoteTtl) {
        super(namespace, minRemoteTtl);
    }

这是第二个文件代码:

@Getter
public class SubConfig2 extends Config {
    private final long refreshValueServiceInterval;


    public SubConfig2(String namespace, long refreshCounterInterval) {
        super(namespace);
        this.refreshValueServiceInterval = refreshCounterInterval;
    }

Config.class

@Getter
public class Config {
    private final int minRemoteTtl;
    private final String namespace;

    public Config(String namespace) {
        this(namespace, 1);
    }

    public Config(String namespace, int minRemoteTtl) {
        this.minRemoteTtl = minRemoteTtl;
        this.namespace = namespace;
    }
}

来自一个软件包的所有配置文件。 我有application.yaml文件:

main:
  config:
    namespace: test
    host: 127.0.0.1
    port: 10000
    maxConnectionsPerEventLoop: 100
    subConfig1:
      minRemoteTtl: 1
    subConfig2:
      refreshCounterInterval: 5


我需要将namespace属性自动连接到SubConfig1SubConfig2中。
我该怎么办?

0 个答案:

没有答案