Spring Boot + Yaml + @PropertySource + @ConfigurationPropert

Spring Boot + Yaml + @PropertySource + @ConfigurationProperties + List in Property Source file not injecting(Spring Boot + Yaml + @PropertySource + @ConfigurationProperties + 属性源文件中的列表未注入)
本文介绍了Spring Boot + Yaml + @PropertySource + @ConfigurationProperties + 属性源文件中的列表未注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)