【问题标题】:Override Spring properties without using profiles在不使用配置文件的情况下覆盖 Spring 属性
【发布时间】:2020-06-22 05:36:44
【问题描述】:

我有几个属性文件(application.properties),其中包含类路径下的自定义和弹簧配置,我想根据系统变量加载这些属性。 我使用PropertySourcesPlaceholderConfigurer 加载如下属性。

@Bean
    public PropertySourcesPlaceholderConfigurer loadResources() {
PropertySourcesPlaceholderConfigurer resourceConfigurer = new PropertySourcesPlaceholderConfigurer();
        resourceConfigurer.setLocations(new ClassPathResource("file1"), new ClassPathResource("file2"));
return resourceConfigurer ;
}
        

但它未能覆盖 spring 配置(在 application.properties 中添加的 spring 配置),我想在 spring 选择 application.properties 文件后覆盖这些文件。 想法是以编程方式覆盖弹簧配置。 如何做到这一点?

【问题讨论】:

  • 为什么?这只会向应用程序添加额外的处理,实际上不会覆盖任何东西(它甚至可能会破坏 Spring Boot 处理)。使用--spring.config.location--spring.config-additional-location 运行应用程序,而不是尝试这样做。请参阅the documentation 了解如何加载其他文件。

标签: spring-boot


【解决方案1】:

我能够使用EnvironmentPostProcessor 实现上述目标。

spring doc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-27
    • 2012-09-05
    • 1970-01-01
    • 2021-03-20
    • 2012-03-07
    • 2021-06-22
    • 2014-01-20
    • 1970-01-01
    相关资源
    最近更新 更多