简体   繁体   English

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

[英]Overwriting Spring Boot application.properties properties in plugin and through Tomcat?

We have a base project, example-platform and an extending customer project, example-customer1 that uses the platform as a dependency. 我们有一个基础项目, example-platform和扩展客户项目, example-customer1 ,它使用平台作为依赖项。 The application.properties of the platform defines the default properties and the customer project overwrites these. 平台的application.properties定义默认属性,客户项目覆盖这些属性。 For example, the platform can be run on its own so it has a spring.datasource.url for the database it uses and the customer project uses another. 例如,平台可以单独运行,因此它有一个spring.datasource.url用于它使用的数据库,而客户项目使用另一个。

Initially, I had hoped that this could be done simply by having an application.properties in the customer project that changed the necessary properties, but instead, only the properties in that file were used instead of anything in the platform's application.properties. 最初,我曾希望这可以通过在客户项目中使用一个更改必要属性的application.properties来完成,而是只使用该文件中的属性而不是平台的application.properties中的任何属性。 At first I just copied over the application.properties file and changed some properties, but I don't like the idea of having to update the customer project if a new property is needed for platform. 起初我只是复制了application.properties文件并更改了一些属性,但我不喜欢在平台需要新属性时必须更新客户项目的想法。 To fix that, I put the application.properties of the customer project into config/application.properties. 为了解决这个问题,我将客户项目的application.properties放入config / application.properties中。 That way I could have the properties from platform and override only the ones I needed to in the customer project. 这样我可以从平台获得属性,并仅覆盖客户项目中需要的属性。

Now I need to be able to override the properties when it's deployed as a War with Tomcat. 现在,我需要能够在将其部署为与Tomcat的战争时覆盖这些属性。 That is, I need properties in example-platform / application.properties to be overridden by example-customer1 / config / application.properties to be overridden by some application.properties for Tomcat. 也就是说,我需要example-platform / application.properties中的属性被example-customer1 / config / application.properties覆盖,以便被Tomcat的某些application.properties覆盖。

Before I put application.properties into the config folder for the customer project, I was able to have an application.properties on Tomcat under props/config and the properties were loaded from that. 在将application.properties放入客户项目的config文件夹之前,我能够在props / config下的Tomcat上安装application.properties,并从中加载属性。 Now that the application.properties of the customer plugin is under /config, however, I think that's being used instead of the properties on Tomcat. 既然客户插件的application.properties在/ config下,我认为正在使用而不是Tomcat上的属性。

What is the most elegant "Spring" way to accomplish this hierarchy of properties? 什么是最优雅的“Spring”方式来实现这种属性的层次结构?

edit: To clarify on what I want, let's say we have a property appName . 编辑:为了澄清我想要的,让我们说我们有一个属性appName In example-platform/application.properties I would have appName=platform-app , in application.properties of example-customer1/application.properties, appName=customer-app . 在example-platform / application.properties中,我将在example-customer1 / application.properties的appName=customer-app中使用appName=platform-appappName=customer-app Then in Tomcat I would like to have an application.properties to be able to override this again, so for one deployment it could be appName=cusomter-app-deployment1 . 然后在Tomcat中我希望有一个application.properties能够再次覆盖它,因此对于一个部署,它可以是appName=cusomter-app-deployment1

The properties are loaded in this order ( source ): 属性按此顺序加载( ):

  • ... ...
  • Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants) 打包jar之外的特定于配置文件的应用程序属性(application- {profile} .properties和YAML变体)
  • Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants) 打包在jar中的特定于配置文件的应用程序属性(application- {profile} .properties和YAML变体)
  • Application properties outside of your packaged jar (application.properties and YAML variants). 打包jar之外的应用程序属性(application.properties和YAML变体)。
  • Application properties packaged inside your jar (application.properties and YAML variants). 打包在jar中的应用程序属性(application.properties和YAML变体)。
  • ... ...

By default, Spring-boot will look for application.properties in your classpath (folder and inside jars) at the locations ./ and ./config (either on disk or in a jar). 默认情况下,Spring-boot将在类路径(文件夹和内部jar)中的位置././config (在磁盘上或jar中)中查找application.properties。

Quick solution 快速解决方案

  • Specify a spring profile ( customer1 ) when initializing your spring boot app 初始化Spring启动应用程序时指定弹簧配置文件( customer1
  • use an example-platform/application.properties 使用example-platform/application.properties
  • and example-customer1/application-customer1.properties . example-customer1/application-customer1.properties

Same solution - long version 相同的解决方案 - 长版本

(1) Platform project (1)平台项目

  • Create your default src/main/resources/application.properties 创建默认的src/main/resources/application.properties
  • Make sure that this application.properties is located in ./config or ./ in your packaged jar. 确保此application.properties位于打包的jar中的./config./中。

(2) Customer1 project (2)Customer1项目

  • Create an application-${profile}.properties for profile customer1 where you'll override the properties: src/main/resources/application-customer1.properties . 为profile customer1创建一个应用程序 - $ {profile} .properties,您将覆盖这些属性: src/main/resources/application-customer1.properties
  • Don't create a default application.properties here. 不要在此处创建默认的application.properties

(3) Launching (3)发射

Whether you launch your spring boot application from a jar or as a deployed war, you need to specify the profile customer1 : 无论是从jar启动Spring启动应用程序还是作为部署战争,您都需要指定配置文件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");
    }
}

That's it. 而已。 Spring Boot will load the default application.properties from its classpasth (example-platform.jar), then load the application-customer1.properties over it. Spring Boot将从其classpasth(example-platform.jar)加载默认的application.properties ,然后在其上加载application-customer1.properties

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

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