简体   繁体   English

如何附加到覆盖的 YAML 文件(Spring)中的数组?

[英]How to append to array in overridden YAML file (Spring)?

I have a JAR which contains an application.yml .我有一个包含application.yml的 JAR。 That YAM file contains an array, eg:该 YAM 文件包含一个数组,例如:

things:
  - name: one
    color: red

I need to adjust this array at runtime by adding an additional application.yml file in the same directory as the JAR.我需要在运行时通过在与 JAR 相同的目录中添加一个额外的application.yml文件来调整这个数组。 However, I'm not sure how to append to the array, because the follow config seems to replace the config in the built-in YAML file in the JAR:但是,我不确定如何附加到数组,因为以下配置似乎替换了 JAR 中内置 YAML 文件中的配置:

things:
  - name: red
    color: blue

In the end, during runtime, I need this:最后,在运行时,我需要这个:

things:
  - name: one
    color: red
  - name: two
    color: blue

There looks like a similar question was asked.似乎有人问过类似的问题 Doesn't sound like it's supported.听起来好像不支持。 Looking through docs, I've found is some older docs show it's not supported.查看文档,我发现一些较旧的文档显示它不受支持。

When a collection is specified in multiple profiles, the one with highest priority is used (and only that one):当在多个配置文件中指定一个集合时,使用具有最高优先级的一个(并且仅使用那个):

 foo: list: - name: my name description: my description - name: another name description: another description --- spring: profiles: dev foo: list: - name: my another name

In the example above, considering that the dev profile is active, FooProperties.list will contain one MyPojo entry (with name “my another name” and description null).在上面的示例中,考虑到开发配置文件处于活动状态,FooProperties.list 将包含一个 MyPojo 条目(名称为“我的另一个名字”且描述为空)。

A newer 2.6.0 snapshot doc mentions the same but seems to encourage you name your keys as their own objects. 较新的 2.6.0 快照文档提到了相同的内容,但似乎鼓励您将键命名为它们自己的对象。 You might not have access to the beans bringing them in but it might be good to structure others in this way if you can.您可能无法访问引入它们的 bean,但如果可以的话,以这种方式构建其他 bean 可能会很好。

Consider the following configuration:考虑以下配置:

 my: map: key1: name: "my name 1" description: "my description 1" --- spring: config: activate: on-profile: "dev" my: map: key1: name: "dev name 1" key2: name: "dev name 2" description: "dev description 2"

If the dev profile is not active, MyProperties.map contains one entry with key key1 (with a name of my name 1 and a description of my description 1).如果开发配置文件未激活,则 MyProperties.map 包含一个键为 key1 的条目(名称为我的名字 1,描述为我的描述 1)。 If the dev profile is enabled, however, map contains two entries with keys key1 (with a name of dev name 1 and a description of my description 1) and key2 (with a name of dev name 2 and a description of dev description 2).但是,如果启用了开发配置文件,则映射包含两个条目,键为 key1(名称为 dev name 1,描述为 my description 1)和 key2(名称为 dev name 2,描述为 dev description 2) .

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

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