【问题标题】:Spring Boot 2.x binding YAML list into class problemsSpring Boot 2.x 绑定 YAML 列表成类问题
【发布时间】:2022-02-01 03:08:31
【问题描述】:

所以我一直在尝试将我的YAML 中的list 属性添加到我创建的类中,但不知何故,列表绑定只能识别来自 YAML 的逗号分隔值。

给定这个类

@Getter
@Configuration
@ConfigurationProperties
@PropertySource("classpath:test.yml")
public class SomeProperty {
    final List<String> someList = new ArrayList<>();
}

还有这个 yaml 文件

someList:
  - a
  - b

如果我在某处autowireSomeProperty 并打印someList,它将返回一个空列表,但如果我将yaml 文件更改为此

someList: a,b

然后绑定起作用并返回一个包含值的数组列表。绑定有什么问题?

【问题讨论】:

  • @pringi 不,这种方法假定我要绑定的 yaml 文件是默认的 application.yml。我想要实现的是绑定另一个外部 yaml 文件。虽然我发现了问题,但它与 PropertySource 相关,因为它只识别键值对的属性格式
  • 这有一个你正在尝试做的例子:stackoverflow.com/questions/34063678/…
  • 是的,不幸的是,这种方法已经被弃用了。 ConfigurationProperties 的 Locations 参数不再存在

标签: spring spring-boot


【解决方案1】:

刚刚发现 PropertySource 只能识别 *.properties 文件,这就是它无法识别 yaml 列表的原因。

【讨论】:

    猜你喜欢
    • 2018-11-18
    • 2018-12-11
    • 2019-11-10
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多