简体   繁体   English

将 Java.Util.Set 加载到 Spring ConfigurationProperties

[英]Loading Java.Util.Set in to Spring ConfigurationProperties

Following Spring @ConfigurationProperties Class遵循 Spring @ConfigurationProperties 类

@Component
@ConfigurationProperties(prefix = "prefix")
@EnableConfigurationProperties
public class config {
    private Set<String> mySet = new HashSet<>();
}

Following Yaml file跟随 Yaml 文件

prefix:
  mySet: !!set {'element1',  'element2'}

Using SnakeYaml version 1.16使用 SnakeYaml 版本 1.16

Results in the follow error:结果出现以下错误:

Caused by: org.springframework.beans.InvalidPropertyException: Invalid property 'messageDelivery[0]' of bean class [location.Config]: Property referenced in indexed property path 'mySet[0]' is neither an array nor a List nor a Map;引起:org.springframework.beans.InvalidPropertyException:bean 类 [location.Config] 的无效属性“messageDelivery[0]”:索引属性路径“mySet[0]”中引用的属性既不是数组也不是列表也不是映射; returned value was [element1]返回值为 [element1]

Appears the error is occurring during serialization出现序列化过程中出现错误

Update更新

I tried with this yaml structure我试过这个 yaml 结构

prefix:
    mySet:
        element1
        element2

This resulted in a Set with only one value which is "element1 element2" it concatenated both together need to find out what will allow me to separate the the elements from each other这导致了一个只有一个值的 Set,它是“element1 element2”,它将两者连接在一起需要找出什么可以让我将元素彼此分开

The Structure that I finally got working needed a comma added我最终得到工作的结构需要添加一个逗号

prefix:
    mySet:
        element1,
        element2

Please try with:请尝试:

prefix:
    ? element1
    ? element2

This should work in order to represent a set in yaml file.这应该可以在 yaml 文件中表示一个集合。 You can see the details here: https://learnxinyminutes.com/docs/yaml/您可以在此处查看详细信息: https : //learnxinyminutes.com/docs/yaml/

I see 2 problems in code you shared.我在您共享的代码中看到了 2 个问题。

  1. mySet is private, I'm wondering how Spring can load data into it mySet是私有的,我想知道 Spring 如何将数据加载到其中
  2. the YAML format is wrong, should be YAML格式错误,应该是
prefix:
   mySet:
     - element1
     - element2

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM