简体   繁体   English

如何使用 bean 构造函数在 Spring 中的 YAML 列表中创建 bean 列表?

[英]How to create list of bean from list in YAML in Spring using the bean constructor?

Based on the question:基于这个问题:
dynamically generate list of beans from .yaml file in confg-server 从 confg-server 中的 .yaml 文件动态生成 bean 列表
I want to create the Server bean using his constructor and not the setter way.我想使用他的构造函数而不是 setter 方式创建服务器 bean

I want a yaml file like:我想要一个yaml文件,如:

test.servers:
    -
      name: test
      url: testurl
    -
      name: test2
      url: test2url

I want to have a class that contains all the servers like:我想要一个包含所有服务器的类,例如:

@ConfigurationProperties(prefix = "test")
@Component
public class RealStateApiClients {
    public List<Server> getServers() {
        return servers;
    }

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

    List<Server> servers = new LinkedList<>();
}

I want to have the class Server like this:我想要这样的类服务器:

public class Server{
    private String name;
    private String url;

    public Server(String name, String url, String pass) {
        this.name = name;
        this.url = url;
    }
}

But when I launch the application I have the exception但是当我启动应用程序时,我有例外

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.Server]: Is it an abstract class?; nested exception is java.lang.InstantiationException: com.Server
    at org.springframework.beans.BeanUtils.instantiate(BeanUtils.java:80) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.AbstractNestablePropertyAccessor.newValue(AbstractNestablePropertyAccessor.java:914) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    ... 33 common frames omitted
Caused by: java.lang.InstantiationException: com.Server
    at java.lang.Class.newInstance(Class.java:427) ~[na:1.8.0_101]
    at org.springframework.beans.BeanUtils.instantiate(BeanUtils.java:77) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    ... 34 common frames omitted
Caused by: java.lang.NoSuchMethodException: com.Server.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_101]
    at java.lang.Class.newInstance(Class.java:412) ~[na:1.8.0_101]
    ... 35 common frames omitted

If I define Server like this, all works:如果我这样定义服务器,一切正常:

public class Server{

    private String name;
    private String url;

    public String getName() {
        return name;
    }

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

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

what I am doing wrong?我做错了什么?
thanks!谢谢!

OMG I made the rookie mistake of spring! OMG 我犯了春天的菜鸟错误!

The Server class must be defined like: Server 类必须定义如下:

public class Server{
    private String name;
    private String url;

    public Server(String name, String url, String pass) {
        this.name = name;
        this.url = url;
    }

    public Server(){}

    public String getName() {
        return name;
    }

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

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

So with a default constructor.所以使用默认构造函数。

Take consideration that the Server class is no initialised with the constructor method and if you want to apply some logic in the constructor this solution does not work, however you can make the Server have a考虑到 Server 类没有用构造函数方法初始化,如果你想在构造函数中应用一些逻辑,这个解决方案不起作用,但是你可以让 Server 有一个

public void init(){
  //do something
}

method and the RealStateApiClients class have a方法和 RealStateApiClients 类有一个

@PostConstruct
public void initAllServers() {
  for (Server server: servers) {
    server.init();
  }
}

And now we have a initialization logic for each Server现在我们有每个服务器的初始化逻辑

Unfortunately @ConfigurationProperties classes have to have:不幸的是, @ConfigurationProperties类必须具有:

  • default constructor默认构造函数
  • setters二传手

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM