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 + 属性源文件中的列表未注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

Sending a keyboard event from java to any application (on-screen-keyboard)(将键盘事件从 java 发送到任何应用程序(屏幕键盘))
How to make JComboBox selected item not changed when scrolling through its popuplist using keyboard(使用键盘滚动其弹出列表时如何使 JComboBox 所选项目不更改)
Capturing keystrokes without focus(在没有焦点的情况下捕获击键)
How can I position a layout right above the android on-screen keyboard?(如何将布局放置在 android 屏幕键盘的正上方?)
How to check for key being held down on startup in Java(如何检查在Java中启动时按住的键)
Android - Get keyboard key press(Android - 获取键盘按键)