无法从 application.yml 加载属性未绑定

我想将服务器属性从 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


绝地无双
浏览 158回答 1
1回答

喵喔喔

您在嵌套静态类中缺少 No arg 构造函数,实际上不需要为构造函数提供参数。只有 setter 和 getter很好public static class Archive {&nbsp; &nbsp; private String host;&nbsp; &nbsp; private String database;&nbsp; &nbsp; private String port;&nbsp; &nbsp; public Archive() {&nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated constructor stub&nbsp; &nbsp; }&nbsp; &nbsp; public Archive(String host, String database, String port) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("constri=uu Archive");&nbsp; &nbsp; &nbsp; &nbsp; this.host = host;&nbsp; &nbsp; &nbsp; &nbsp; this.database = database;&nbsp; &nbsp; &nbsp; &nbsp; this.port = port;&nbsp; &nbsp; }&nbsp; &nbsp; public String getHost() {&nbsp; &nbsp; &nbsp; &nbsp; return host;&nbsp; &nbsp; }&nbsp; &nbsp; public void setHost(String host) {&nbsp; &nbsp; &nbsp; &nbsp; this.host = host;&nbsp; &nbsp; }&nbsp; &nbsp; public String getDatabase() {&nbsp; &nbsp; &nbsp; &nbsp; return database;&nbsp; &nbsp; }&nbsp; &nbsp; public void setDatabase(String database) {&nbsp; &nbsp; &nbsp; &nbsp; this.database = database;&nbsp; &nbsp; }&nbsp; &nbsp; public String getPort() {&nbsp; &nbsp; &nbsp; &nbsp; return port;&nbsp; &nbsp; }&nbsp; &nbsp; public void setPort(String port) {&nbsp; &nbsp; &nbsp; &nbsp; this.port = port;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Archive [host=" + host + ", database=" + database + ", port=" + port + "]";&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java