Spring Boot + Yaml + @PropertySource + @ConfigurationProperties + 属性源文件中的列表未注入

2022-01-14 00:00:00 yaml java spring-boot

我正在尝试使用 @ConfigurationPropertiesexternal.yml 属性注入 POJO,并使用 导入我的 external.yml 文件>@PropertySource.所有其他都被注入到 POJO 中,但不是一个复杂的列表.

I'm trying to inject external.yml properties into a POJO using @ConfigurationProperties and importing my external.yml file using @PropertySource. All other were injected into POJO but not a complex list.

一个.我尝试将 @NestedConfigurationProperty 用于复杂列表和地图.湾.我尝试将复杂的 POJO 作为内部静态和外部类.C.尝试了 @EnableConfigurationProperties 注释.等等

a. I tried using @NestedConfigurationProperty for the complex list and map. b. I tried complex POJO as inner static and outer class. c. Tried @EnableConfigurationProperties annotation. etc

ExternalProp.java

   @Component
   @PropertySource(value = "classpath:external.yml", encoding = "UTF-8")
   @ConfigurationProperties

   @Data
   public class ExternalProp {

    private String bla;
    private List<Person> persons;
    private List<String> other;

    @Data
    public static class Person {

        private int age;
        private String name;
        private Map<String, String> args;
    }

}

external.yml

   bla: bkdfjgkdf
   persons:
   -  age: 12
      name: bla1
      args:
        a0: a0dev
        a1: a1dev
        a2: a2dev
   -  age: 12
      name: bla2
      args:
        b0: b0dev
        b1: b1dev
        b2: b2dev
   strings: bla, bla1

bla: bkdfjgkdfstrings: bla, bla1 已正确注入,但未注入人员.我在哪里犯错了.

bla: bkdfjgkdf and strings: bla, bla1 have been injected properly but not persons. Where am I making mistake.

同样的 persons 对象复制到 application.yml 文件,然后,BOOM,正确获取值.

And same persons object copied to application.yml file, then, BOOM, getting values properly.

我期待一个包含一些地图 args 的人员列表.

I'm expecting a List of Persons which is having some Map args.

推荐答案

我相信这与你的 yml 格式有关.试试这个

I believe it has to do with your yml formatting. try this

external.yml

   bla: bkdfjgkdf
   persons:
      -  age: 12
         name: bla1
         args:
           a0: a0dev
           a1: a1dev
           a2: a2dev
      -  age: 12
         name: bla2
         args:
           b0: b0dev
           b1: b1dev
           b2: b2dev
   strings: bla, bla1

相关文章