Spring Boot @ConfigurationProperties not retrieving properties from Environment

后端 未结 2 1877
自闭症患者
自闭症患者 2021-02-13 14:20

I\'m using Spring Boot 1.2.1 and trying to create a @ConfigurationProperties bean with validation like so:

package com.sampleapp;

import java.net.U         


        
2条回答
  •  北恋
    北恋 (楼主)
    2021-02-13 14:56

    It's clearly written here: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties

    Getters and setters are usually mandatory, since binding is via standard Java Beans property descriptors, just like in Spring MVC. There are cases where a setter may be omitted: Maps, as long as they are initialized, need a getter but not necessarily a setter since they can be mutated by the binder. Collections and arrays can be either accessed via an index (typically with YAML) or using a single comma-separated value (properties). In the latter case, a setter is mandatory. We recommend to always add a setter for such types. If you initialize a collection, make sure it is not immutable (as in the example above) If nested POJO properties are initialized (like the Security field in the example above), a setter is not required. If you want the binder to create the instance on-the-fly using its default constructor, you will need a setter. Some people use Project Lombok to add getters and setters automatically. Make sure that Lombok doesn’t generate any particular constructor for such type as it will be used automatically by the container to instantiate the object.

提交回复
热议问题