@ConfigurationProperties实现自定义配置绑定

@ConfigurationProperties使用

@ConfigurationProperties(

        prefix = "hello.properties"

)

public class MyProperties {


    private String myKey;

    private List<String> stringList;

    private Duration duration;


    public String getMyKey() {

        return myKey;

    }


    public void setMyKey(String myKey) {

        this.myKey = myKey;

    }


    public List<String> getStringList() {

        return stringList;

    }


    public void setStringList(List<String> stringList) {

        this.stringList = stringList;

    }


    public Duration getDuration() {

        return duration;

    }


    public void setDuration(Duration duration) {

        this.duration = duration;

    }


    @Override

    public String toString() {

        return "MyProperties{" +

                "myKey='" + myKey + '\'' +

                ", stringList=" + stringList +

                ", duration=" + duration +

                '}';

    }

}
View Code

prefix属性是配置文件里的前缀,即配置文件中以前缀 + 变量名的形式配置一条记录,来对应类中的一个变量,如下:

hello.properties.myKey=hello

hello.properties.duration=20s

hello.properties.string-list[0]=Acelin

hello.properties.string-list[1]=nice

@ConfigurationProperties特点

hello.properties.myKey=hello

hello.properties.mykey=hello

hello.properties.my-key=hello

hello.properties.my_key=hello

hello.properties.MY_KEY=hello

hello.properties.MY-KEY=hello

https://www.cnblogs.com/acelin/p/15167266.html

 

posted @ 2021-08-29 18:25  Bonnie_ξ  阅读(97)  评论(0编辑  收藏  举报