[英]Load a List type property from application yml into Java POJO
I want to load a nested List property into a JAVA POJO 我想将嵌套的List属性加载到JAVA POJO中
I am loading the properties from yml using ConfigurationProperties
annotation over a class A. My List is of type B. This B object has a LIST attribute of it's own. 我正在使用类A上的
ConfigurationProperties
注释从yml加载属性。我的列表的类型为B。此B对象具有自己的LIST属性。 However yml properties are not getting loaded as expected. 但是,yml属性未按预期加载。
@ConfigurationProperties(prefix="prop")
public class A{
List<B> b = new ArrayList<>();
//getters and setters ......
}
public class B{
String user; //This property gets loaded.
List<String> list = new ArrayList<>(); //However this list is still empty
//getters and setters ......
}
My properties in application.yml
looks like below. 我在
application.yml
属性如下所示。
prop:
-
user: alpha
list: a,b,c
-
user: beta
list: x,y,z
Well this is not the YAML syntax of a List: 好吧,这不是List的YAML语法:
list: a,b,c
That is just one string a,b,c
. 那只是一个字符串
a,b,c
。
If you wish to use a coma delimited list, you can parse it after you load it. 如果希望使用逗号分隔列表,则可以在加载后对其进行解析。 Spring does something similar with some of its own properties, like in this example with RabbitMQ properties.
Spring使用自己的某些属性执行类似的操作,例如在此示例中使用RabbitMQ属性。 The
addresses
are coma delimited, and the function parseAddresses()
splits the string after loading, as part of the setter method of that member. addresses
以parseAddresses()
分隔,并且函数parseAddresses()
在加载后拆分字符串,作为该成员的setter方法的一部分。
https://github.com/spring-projects/spring-boot/blob/v2.1.3.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java https://github.com/spring-projects/spring-boot/blob/v2.1.3.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ AMQP / RabbitProperties.java
Otherwise use the YAML list syntax. 否则,请使用YAML列表语法。
prop:
- user: alpha
list:
- a
- b
- c
- user: beta
list:
- x
- y
- z
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.