Spring Boot: read list from yaml using @Value or @ConfigurationProperties

后端 未结 4 815
旧时难觅i
旧时难觅i 2020-12-28 08:25

I want to read a list of hosts from a yaml file (application.yml), the file looks like this:

cors:
    hosts:
        allow: 
            - http://foo1/
             


        
4条回答
  •  生来不讨喜
    2020-12-28 08:47

    I have been able to read list from properties like below way-

    Properties-

    cors.hosts.allow[0]=host-0
    cors.hosts.allow[1]=host-1
    

    Read property-

    @ConfigurationProperties("cors.hosts")
    public class ReadProperties {
        private List allow;
    
        public List getAllow() {
           return allow;
        }
        public void setAllow(List allow) {
            this.allow = allow;
        }
    }
    

提交回复
热议问题