dynamically generate list of beans from .yaml file in confg-server

自闭症网瘾萝莉.ら 提交于 2020-01-03 05:07:29

问题


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;

then:

@Component
public class Server {

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

in the (applicationName).yaml file:

servers:
-
    name: test
-
    name: test2

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?

Thanks for any help


回答1:


we figured this out...

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;


来源:https://stackoverflow.com/questions/28181218/dynamically-generate-list-of-beans-from-yaml-file-in-confg-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!