Spring Boot + Yaml + @PropertySource + @ConfigurationProperties +属性源文件中的列表未注入 [英] Spring Boot + Yaml + @PropertySource + @ConfigurationProperties + List in Property Source file not injecting

查看:74
本文介绍了Spring Boot + Yaml + @PropertySource + @ConfigurationProperties +属性源文件中的列表未注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 @ConfigurationProperties external.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.

a.我尝试将 @NestedConfigurationProperty 用于复杂的列表和映射.b.我尝试将复杂的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:bkdfjgkdf strings: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

这篇关于Spring Boot + Yaml + @PropertySource + @ConfigurationProperties +属性源文件中的列表未注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆