【发布时间】:2021-07-11 05:30:06
【问题描述】:
在春季,如果我们有两个 .yaml 文件。 (包含在 application.yaml 中)
application-one.yaml application-two.yaml
------------------- -------------------
abc: abc:
flow: flow:
- id: 'remote02' - id: 'remote04'
- id: 'remote03' - id: 'remote05'
如果这个配置的属性类是这样的-
@ConfigurationProperties(prefix = "abc")
class Abc {
List<Flow> flow;
}
class Flow{
String id;
}
然后将仅从一个文件加载“流”,即 flow.size() 给出 2。(remote04,remote05。这取决于 application.yaml 中的配置文件顺序)
但如果我将 yamls 文件更改为 -
application-one.yaml application-two.yaml
------------------- -------------------
abc: abc:
flow: flow:
remote02: remote04:
id: remote02 id: remote04
remote03: remote05:
id: remote03 id: remote05
和属性类
@ConfigurationProperties(prefix = "abc")
class Abc {
HashMap<String, Flow> flow;
}
class Flow{
String id;
}
现在 hashmap 将有 4 个条目。即 flow.size() 给出 4
那么这是否意味着 spring 不会从两个 yaml 中加载 List 结构并将它们添加到一起?
如果属实,如何克服这个限制?
【问题讨论】:
-
我只是在这里猜测,但我认为这可能与 yaml 中的“一切都是地图”这一事实有关,但并非一切都是列表。所以列表是 1 个对象,它在 yaml 中具有相同的键,因此它被第一个或最后一个加载的“否决”(不确定是哪个)。但是,“地图”可以合并,因为无论如何这是默认行为。
标签: java spring spring-boot yaml