java - 在插件中和通过 Tomcat 覆盖 Spring Boot application.properties 属性?

标签 java spring spring-mvc tomcat spring-boot

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

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

现在我需要能够在部署为与 Tomcat 的 war 时覆盖属性。也就是说,我需要 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

最佳答案

属性按以下顺序加载(source):

  • ...
  • Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)
  • Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants)
  • Application properties outside of your packaged jar (application.properties and YAML variants).
  • Application properties packaged inside your jar (application.properties and YAML variants).
  • ...

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

快速解决方案

  • 在初始化 spring boot 应用时指定 spring 配置文件(customer1)
  • 使用example-platform/application.properties
  • example-customer1/application-customer1.properties

相同的解决方案 - 长版

(1)平台项目

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

(2) Customer1 项目

  • 为配置文件 customer1 创建一个 application-${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

关于java - 在插件中和通过 Tomcat 覆盖 Spring Boot application.properties 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39520888/

相关文章:

java - 如何将 jax-ws 服务部署到 eclipse 或 tomcat?

java - 将 ADB 添加到我的 Java PC 应用程序

java - 为什么@PostConstruct 和@Retryable 不能一起使用?

internet-explorer - IE 仅在 404 和 500 页面上不显示图标。什么是可能是错误的?

java - 如何将 Java 配置文件添加到 Eclipse 的 Papyrus 2.0.0 插件?

java - 正则表达式在java中提供额外的输出

java - Spring bean 在创建时抛出错误

java - @PathVariable 编码问题 - Spring MVC

spring-mvc - spring mvc 使用 session 登录/注销

java - Spring Security 3.2 和 maximumSessions - 注销不更新 SessionRegistry