java - 无法从 application.yml 加载属性未绑定(bind)

标签 java spring spring-boot

我想将服务器属性从 application.yml 加载到 Configuration class 。
我看到很多人已经问过同样的问题,但没有一个对我有用:(
请帮我弄清楚我错过了什么

@Configuration
  @ConfigurationProperties("demo")
  public class Democonfig {
    private List<Archive> archive = new ArrayList<>();
    public Democonfig(List<Archive> archive) {
        this.archive = archive;
    }
    // Getter and setter
   public static class Archive {
        private String host;
        private String database;
        private String port;

        public Archive(String host, String database, String port) {
            this.host = host;
            this.database = database;
            this.port = port;
        }
           // Getters and setters
   }
}

application.yml
demo:
  archive:
  -
    host: "localhost"
    database: "archive1"
    port: "27017"
  -
    host: "localhost"
    database: "archive2"
    port: "27017" 

它的显示异常
Binding to target [Bindable@129425f type = java.util.List<com.example.demo.config.Democonfig$Archive>, value = 'provided', annotations = array<Annotation>[[empty]]] failed:

    Property: demo.archive[0].database
    Value: archive1
    Origin: class path resource [application.yml]:5:15
    Reason: The elements [demo.archive[0].database,demo.archive[0].host,demo.archive[0].port,demo.archive[1].database,demo.archive[1].host,demo.archive[1].port] were left unbound.
    Property: demo.archive[0].host
    Value: localhost
    Origin: class path resource [application.yml]:4:11
    Reason: The elements [demo.archive[0].database,demo.archive[0].host,demo.archive[0].port,demo.archive[1].database,demo.archive[1].host,demo.archive[1].port] were left unbound.
    Property: demo.archive[0].port
    Value: 27017

点击here !
获取在 git 中上传的完整源代码和项目

最佳答案

您缺少嵌套静态类中的 No arg 构造函数,实际上是 无需为构造函数提供参数。只有 setter 和 getter 可以 我也鼓励你检查并使用 lombok,它会让你的代码更加简洁

public static class Archive {
    private String host;
    private String database;
    private String port;

    public Archive() {
        // TODO Auto-generated constructor stub
    }

    public Archive(String host, String database, String port) {
        System.out.println("constri=uu Archive");
        this.host = host;
        this.database = database;
        this.port = port;
    }
    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
    public String getDatabase() {
        return database;
    }
    public void setDatabase(String database) {
        this.database = database;
    }
    public String getPort() {
        return port;
    }
    public void setPort(String port) {
        this.port = port;
    }
    @Override
    public String toString() {
        return "Archive [host=" + host + ", database=" + database + ", port=" + port + "]";
    }

}

关于java - 无法从 application.yml 加载属性未绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54505586/

相关文章:

java - 如何将我的 SpringBoot 后端连接到 flutter?

mysql - 数据库正在覆盖。来自 json 的最后一个条目写入数据库,所有其他条目都被覆盖。仅显示 Json 文件的最后一个条目

java - 如何将 jar 文件输出返回给 ProcessBuilder?

java - Hibernate 用于获取子选择的原始整数列表

java - 如何将 BiPredicate 与 anyMatch() 一起使用

hibernate - 当@Transactional 处于类级别时如何使用只读

spring - 在 Spring 应用程序中运行 junit 测试时访问 h2 Web 控制台

windows - gradle 为 windows 生成的 startScript 产生 "input line is too long"

java - 线程执行指定的时间。

java - 具有不包括静态的上下文路径的Spring Boot