简体   繁体   English

从应用程序yml将列表类型属性加载到Java POJO中

[英]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. addressesparseAddresses()分隔,并且函数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

According to the manual you define your list items as: 根据手册,您将列表项定义为:

list: 
      - a
      - b
      - c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从application.yml读取配置到POJO列表列表 - Read config from application.yml to Map of List of POJO java.lang.IllegalStateException:无法从位置“类路径:/application.yml”加载属性源 - java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml' 无法从位置“ classpath:/application.yml”加载属性源 - Failed to load property source from location 'classpath:/application.yml' Java 8-如何将Pojo转换为其嵌入式Pojo属性的列表 - Java 8 - How convert Pojo into list of its embedded Pojo property Json使用嵌套列表序列化为Java Pojo <pojo> 属性 - Json serialize to java pojo with nested List<pojo> property 在JSP中从列表设置POJO的属性 - Setting property of POJO from List in JSP 从 Micronaut 中的 application.yml 访问属性 - Access Property from application.yml in Micronaut 是否可以将 application.yml 中的属性解析为具有不同名称的 java 类字段? - Is it possible to parse a property from application.yml to the java class field with a different name? 如何将 application.yml 中的类属性与具有不同类名的 java 类匹配? - How to match a class property from application.yml to the java class with a different class name? 如何将yml文件转换为java pojo - how to convert yml file to java pojo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM