@ConfigurationProperties 获取配置的值为 null

357 阅读1分钟

问题

在获取配置文件中的属性时,使用 ConfigurationProperties 注解后,发现获取到的值为 null .

解决方法

使用 lombok 的 data 注解,来将配置文件中的值注入到属性中.

看了一下注解的注释,好像是通过 setter 方法来注入到属性中的, 所以使用 data 注解就可以附上值了

Binding is either performed by calling setters on the annotated class or,
这段话应该就是说通过 setters 方法来注入的... 具体怎么执行的也没有太搞明白

Annotation for externalized configuration. Add this to a class definition or a @Bean method in a @Configuration class if you want to bind and validate some external Properties (e.g. from a .properties file).
Binding is either performed by calling setters on the annotated class or, if @ConstructorBinding is in use, by binding to the constructor parameters.
Note that contrary to @Value, SpEL expressions are not evaluated since property values are externalized