亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法從 application.yml 加載屬性未綁定

無法從 application.yml 加載屬性未綁定

絕地無雙 2022-06-04 15:05:35
我想將服務器屬性從 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.ymldemo:  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
查看完整描述

1 回答

?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

您在嵌套靜態類中缺少 No arg 構造函數,實際上不需要為構造函數提供參數。只有 setter 和 getter很好


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 + "]";

    }


}


查看完整回答
反對 回復 2022-06-04
  • 1 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號