【问题标题】:Overwriting Spring Boot application.properties properties in plugin and through Tomcat?在插件中和通过 Tomcat 覆盖 Spring Boot application.properties 属性?
【发布时间】:2017-01-24 01:22:17
【问题描述】:

我们有一个基础项目example-platform 和一个扩展客户项目example-customer1,它使用平台作为依赖项。平台的 application.properties 定义了默认属性,客户项目将覆盖这些属性。例如,平台可以独立运行,因此它使用的数据库有一个spring.datasource.url,而客户项目使用另一个。

最初,我希望这可以通过在客户项目中使用更改必要属性的 application.properties 来完成,但相反,只使用该文件中的属性而不是平台的 application.properties 中的任何内容.起初我只是复制了 application.properties 文件并更改了一些属性,但我不喜欢在平台需要新属性时必须更新客户项目的想法。为了解决这个问题,我将客户项目的 application.properties 放入 config/application.properties。这样我就可以拥有来自平台的属性,并且只覆盖我在客户项目中需要的那些。

现在我需要能够在将其部署为与 Tomcat 的战争时覆盖这些属性。也就是说,我需要 example-platform / application.properties 中的属性被 example-customer1 / config / application.properties 覆盖,以便被 Tomcat 的某些 application.properties 覆盖。

在我将 application.properties 放入客户项目的 config 文件夹之前,我能够在 Tomcat 上的 props/config 下拥有一个 application.properties,并从中加载属性。现在客户插件的 application.properties 位于 /config 下,但是,我认为正在使用它而不是 Tomcat 上的属性。

完成这种属性层次结构的最优雅的“Spring”方式是什么?

编辑:为了澄清我想要什么,假设我们有一个属性appName。在 example-platform/application.properties 中,我将拥有 appName=platform-app,在 example-customer1/application.properties 的 application.properties 中,appName=customer-app。然后在 Tomcat 中,我希望有一个 application.properties 能够再次覆盖它,因此对于一个部署,它可能是appName=cusomter-app-deployment1

【问题讨论】:

  • 这个问题有点不清楚,但听起来像是“使用环境变量或系统属性”。
  • @chrylis,这是一种选择,但将它放在 application.properties 文件中会更好。我会更新问题以使其更清楚
  • 你看过 Spring 的关于属性覆盖优先级的文档吗?这可能就是您正在寻找的。​​span>

标签: java spring spring-mvc tomcat spring-boot


【解决方案1】:

按此顺序加载属性 (source):

  • ...
  • 打包 jar 之外的特定于配置文件的应用程序属性(application-{profile}.properties 和 YAML 变体)
  • 打包在您的 jar 中的特定于配置文件的应用程序属性(application-{profile}.properties 和 YAML 变体)
  • 打包 jar 之外的应用程序属性(application.properties 和 YAML 变体)。
  • 应用程序属性打包在您的 jar 中(application.properties 和 YAML 变体)。
  • ...

默认情况下,Spring-boot 将在您的类路径(文件夹和 jar 内)中的 ././config 位置(在磁盘上或在 jar 中)查找 application.properties。

快速解决方案

  • 在初始化您的 Spring Boot 应用时指定一个弹簧配置文件 (customer1)
  • 使用example-platform/application.properties
  • example-customer1/application-customer1.properties

相同的解决方案 - 长版

(1) 平台项目

  • 创建您的默认src/main/resources/application.properties
  • 确保此 application.properties 位于您打包的 jar 中的 ./config./

(2) Customer1 项目

  • 为配置文件customer1 创建一个应用程序-${profile}.properties,您将在其中覆盖属性:src/main/resources/application-customer1.properties
  • 不要在此处创建默认的application.properties

(3) 启动

无论您是从 jar 启动 Spring Boot 应用程序还是作为部署的 War,您都需要指定配置文件 customer1

@SpringBootApplication
public class SpringBootApp extends SpringBootServletInitializer{
    public static void main(String[] args){
        new SpringApplicationBuilder() //
        .sources(SpringBootApp.class)//
        .profiles("customer1")
        .run(args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SpringBootApp.class)//
        .profiles("customer1");
    }
}

就是这样。 Spring Boot 将从其类路径(example-platform.jar)加载默认的application.properties,然后在其上加载application-customer1.properties

【讨论】:

    猜你喜欢
    • 2020-06-22
    • 1970-01-01
    • 2018-04-12
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2014-06-27
    相关资源
    最近更新 更多