将列表类型属性从应用程序yml加载到Java POJO中
我想将嵌套列表属性加载到JAVA POJO中 我在类a上使用ConfigurationProperties注释从yml加载属性。我的列表类型为B。此B对象具有自己的列表属性。但是,yml属性未按预期加载将列表类型属性从应用程序yml加载到Java POJO中,java,spring,spring-boot,yaml,Java,Spring,Spring Boot,Yaml,我想将嵌套列表属性加载到JAVA POJO中 我在类a上使用ConfigurationProperties注释从yml加载属性。我的列表类型为B。此B对象具有自己的列表属性。但是,yml属性未按预期加载 @ConfigurationProperties(prefix="prop") public class A{ List<B> b = new ArrayList<>(); //getters and setters ...... } public class B{
@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 ......
}
根据定义,您可以将列表项定义为:
list:
- a
- b
- c
这不是列表的YAML语法: 名单:a、b、c 这只是一个字符串a,b,c 如果希望使用逗号分隔的列表,可以在加载后对其进行解析。 Spring对它自己的一些属性做了类似的事情,比如本例中的RabbitMQ属性。地址是分隔的,作为该成员setter方法的一部分,函数parseAddresses在加载后拆分字符串 否则,请使用YAML列表语法
prop:
- user: alpha
list:
- a
- b
- c
- user: beta
list:
- x
- y
- z
即使这样,它也不起作用。它无法提取嵌套列表并对其进行初始化。@jbx给出的语法对于嵌套容器是正确的。您必须在命名、嵌套或为成员提供访问器时出错。
prop:
- user: alpha
list:
- a
- b
- c
- user: beta
list:
- x
- y
- z