使用bootstrap.yml覆盖命令行的Spring属性

问题描述

我有一个tomcat Box,它已经设置了spring活动配置文件,作为每次通过catalina部署spring app时的命令行参数。

我正在使用Spring Cloud配置服务器,因此在配置客户端中,我在bootstrap.yml中指定了活动配置文件,但是正如我之前提到的,它被tomcat命令行参数覆盖。

如何在引导上下文加载时使用我的boostrap.yml覆盖通过tomcat传递的命令行参数,以便我可以将活动配置文件从bootstrap.yml传递给配置服务器。

Tomcat设置环境命令(由于无法访问,我无法更改)

autocapitalize: 'characters'

bootstrap.yml

JAVA_OPTS="$JAVA_OPTS -Djava.library.path=/path -Dspring.profiles.active=e2"

解决方法

命令行参数(-Dspring.profiles.active = e2)将始终覆盖属性文件,无论您在yaml文件中指定了多少个硬编码配置文件。我建议您添加要在运行时以编程方式设置的其他配置文件,并在.yml扩展名之前保留两个带有-profilename的属性文件。

这可以按照以下步骤进行:

ApplicationMain.java

public static void main(String[] args) {
    SpringApplication app = new SpringApplication(DemoApplication.class);
    app.setAdditionalProfiles("cron");
    app.run(args);
}

bootstrap-e2.yml

// Keep all the properties which is specific to e2 profile.

bootstrap-cron.yml

   // Keep all the properties which is specific to cron profile.

这样,您可以同时使用两个配置文件。但是,如果两个引导文件中都有一个公共属性,则程序将从该引导文件中选择其属性profile与运行时args匹配。 :-Dspring.profiles.active