从 confg-server 中的 .yaml 文件动态生成 bean 列表 [英] dynamically generate list of beans from .yaml file in confg-server

查看:23
本文介绍了从 confg-server 中的 .yaml 文件动态生成 bean 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring-config-server.我让它工作了,但理想情况下,我想在具有属性的 .yaml 文件中生成服务器列表.

I'm using spring-config-server. I have it working, but Ideally, I'd like to generate a list of servers in a .yaml file that have properties.

@Resource
private List<Server> servers;

然后:

@Component
public class Server {

      @Value("${server.name}")
       private String name;
}

在 (applicationName).yaml 文件中:

in the (applicationName).yaml file:

servers:
-
    name: test
-
    name: test2

请参阅我想要从配置动态生成的 List.这个配置在配置服务器上的事实不应该与本地 .yaml 文件有什么不同,对吗?

See I want a List<Server> generated dynamically from a config. The fact that this config is on a config server shouldn't be that different from a local .yaml file right?

感谢您的帮助

推荐答案

我们想通了...

Trinity:
  test: Goober
  servers:
    -
      name: test
      jmxURL: jmx://test
    -
      name: test2
      jmxURL: jmx://test

这是配置(在配置服务器中)...这是代码

that's the config (in config server)... this is the code

@Component
@EnableAutoConfiguration
@EnableConfigurationProperties
@ConfigurationProperties(prefix="Trinity")
public class ConfigFetcher {


    List<Server> servers;

    public List<Server> getServers() {
        return servers;
    }

    public void setTest(String test) {
        this.test = test;
    }


    public void setServers(List<Server> servers) {
        this.servers = servers;
    }


    @EnableConfigurationProperties
    @ConfigurationProperties(prefix="Trinity.servers")
    public static class Server{
        private String name;
        private String jmxURL;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }


        public String getJmxURL() {
            return jmxURL;
        }

        public void setJmxURL(String jmxURL) {
            this.jmxURL = jmxURL;
        }

        @Override
        public String toString() {
            return "Server [name=" + name + ", jmxUrl=" + jmxURL + "]";
        }

    }   

}

在主类中(在我的服务中,在这种情况下):具有以下注释的类

in the main class (in my service in this case): with the class having the following annotation

 @EnableAutoConfiguration


    @Autowired
    private ConfigFetcher c;

这篇关于从 confg-server 中的 .yaml 文件动态生成 bean 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆