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 中上传的完整源代码和项目

最佳答案

您在嵌套静态类中缺少无参数构造函数,实际上不需要为构造函数提供参数。只有 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/60697491/

相关文章:

java - Spring Boot 的不可变配置

java - 在 Spring boot fat jar 中读取文件

java - 在 mongo 文档中创建自动增量字段

angularjs - 在 AngularJS 中访问 Spring MVC 模型对象

Java 服务器-客户端游戏

java - 在 spring boot 中动态更改 application.properties 值

java - ISO 8601、JsonFormat、TimeZone 和末尾的 +0000 出现 InvalidFormatException

java - 连接 Spring Boot 应用程序和 google Cloud SQL 的推荐方式是什么?

Java LED 编程

java - 关于使用 Scanner 类创建类、设置方法和 String 数据类型的问题