【问题标题】:How to represent Spring properties, with a value and sub-properties, in YML?如何在 YML 中用值和子属性表示 Spring 属性?
【发布时间】:2021-03-28 15:32:37
【问题描述】:

我正在使用一个使用嵌套属性进行配置的库。

例如

@Value("${foo.bar}") String fooBar;
@Value("${foo.bar.baz}") String fooBarBaz;

这可以在以下属性文件中成功表示:

foo.bar=abc
foo.bar.baz=xyz

但是,我一直无法找到在 YML 文件中表示此内容的方法。这甚至可以做到吗? 我尝试了以下方法:

// Invalid (ScannerException).
foo:
  bar: abc
    baz: xyz

// Invalid (NullPointerException).
foo:
  bar:
    ~: abc
    baz: xyz

// Invalid (DuplicateKeyException).
foo:
  bar: abc
  bar:
    baz: xyz

【问题讨论】:

    标签: spring spring-boot properties yaml properties-file


    【解决方案1】:

    实际上,我刚刚意识到这可以通过将嵌套折叠到键中来完成。

    例如

    foo:
      bar: abc
      bar.baz: xyz
    

    【讨论】:

      猜你喜欢
      • 2018-08-21
      • 2017-05-15
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 2017-09-07
      相关资源
      最近更新 更多