Spring Boot - YML配置文件 - 合并时删除条目

3

我有一个基础的YML配置文件,位于类路径中,内容如下:

hello-world:
  values:
    bar:
      name: bar-name
      description: bar-description
    foo:
      name: foo-name
      description: foo-description

hello-world包含一个从字符串到POJO的映射,称为values。我想覆盖hello-world中的设置,特别是我想删除一个条目。因此,在我运行应用程序的本地目录上,我有这个application.yml文件:

hello-world:
  values:
    bar:
      name: bar-name
      description: bar-description

source: from-the-local-dir

但这不起作用,因为当我的本地配置覆盖现有配置时,映射被合并成一个,并保留原始条目“foo”。有没有一种方法可以在spring yml中明确删除配置映射中的条目?
PS:我可以通过修改本地文件中的“bar”条目来看到本地文件被拾取。以下是完整代码,我已经添加了一个“source”配置来告诉最后加载的文件是哪一个:
@Import(PlayGround.Config.class)
@SpringBootApplication
public class PlayGround {

    @Autowired
    Config config;

    @Value("${source}")
    String source;

    public void start() {
        System.out.println(config);
        System.out.println(source);
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(args));
        ConfigurableApplicationContext context = SpringApplication.run(PlayGround.class, args);
        PlayGround playGround = context.getBean(PlayGround.class);
        playGround.start();
    }

    @ConfigurationProperties(prefix = "hello-world")
    public static final class Config {
        Map<String, Information> values = new HashMap<String, Information>();

        public Map<String, Information> getValues() {
            return values;
        }

        public void setValues(Map<String, Information> values) {
            this.values = values;
        }

        @Override
        public String toString() {
            return MoreObjects.toStringHelper(this)
                    .add("values", values)
                    .toString();
        }
    }

    public static final class Information {

        String name;
        String description;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        @Override
        public String toString() {
            return MoreObjects.toStringHelper(this)
                    .add("name", name)
                    .add("description", description)
                    .toString();
        }
    }

}
1个回答

2

Spring Boot默认从src/main/resources/application.yml中读取文件。您可以声明config/application.yml文件,这些配置将覆盖src/main/resources中的application.yml。

您只需尝试使用src/main/resources/application.yml即可:

hello-world:
  bar: 
    name: bar-name
    description: bar-description
  foo: 
    name: foo-name
    description: foo-description

And config/application.yml

  hello-world:
      bar: 
        name: bar-name
        description: bar-description

我认为这可以帮助你。

当你运行你的应用程序时,config/application.yml将覆盖现有的src/main/resources/application.yml。

你可以完全从config/application.yml中删除hello-world,但它会抛出一个运行时异常,类似于:

Could not resolve placeholder 'hello-world.foo' in value "${hello-world.foo}

为了解决这个问题,您可以使用注入值的方式进行修复,例如: @Value("${hello-world.foo:}") 在“:”后面,您可以定义默认值。
您可以将config/application.yml中的字段留空。
hello-world:
  bar: 
    name: bar-name
    description: bar-description
  foo: 
    name:
    description:

默认情况下,如果您没有指定foo中的所有值,则所有值都将为空(''),然后您可以过滤并从地图中删除具有空值的条目。 另外,您可以查看此类:https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/YamlProcessor.ResolutionMethod.html

谢谢你的回答。我的问题是合并过程会将两个地图合并在一起。因此,来自src/main/resources/application.yml的地图将被config/application.yml中的条目更新。如果我想改变'bar'条目的内容,它可以正常工作,但如果我想完全删除bar,它就不起作用了。 - undefined
在config/application.yml文件中,完全不要声明hello-world。 - undefined
嗨,我再次来到这里。这样做并不够。我正在尝试找到一种删除设置映射中条目的方法。我已编辑我的答案以更清楚地表达。我可以看到我可以使用本地的application.yml覆盖我的设置,但是我还没有找到一种明确删除映射条目的设置文件的方法。类路径目录中的映射条目总是存在。 - undefined
好的,那么唯一的方法就是将值留空并手动删除它们。关于ResolutionMethod,你如何指定要使用哪个? - undefined
我不确定YamlParser的工作原理,可能需要进行深入调查。谢谢。 - undefined
我可以调查一下,如果你想的话。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接