Search code examples
springspring-bootspring-cloudspring-cloud-configspring-context

override command line Spring properties with bootstrap.yml


i have a tomcat box , which has spring active profile set already, as a command line argument every time a spring app is deployed through catalina.

i am using spring cloud config server , so in config client i specify active profile in bootstrap.yml , but as i mention earlier it is overriden by tomcat command line argument .

how to override the command line argument passed through tomcat , with my boostrap.yml at the time of bootstrap context loading so that i can pass active profile from my bootstrap.yml to config server.

Tomcat set environment command (which i cannot change as i dont have access)

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

bootstrap.yml

spring:
  profiles:
    active: e2,cron
  cloud:
    config:
      uri: http://localhost:8888
  application:
    name: heartbeat_monitor. 

Solution

  • Command line argument(-Dspring.profiles.active=e2) will always override your properties file, no matter how many hardcoded profiles you specify in your yaml file. I would suggest you to add additional profile to be set programatically at the runtime and keep two property files with -profilename before the .yml extension.

    This could be done as follows:

    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.
    

    In this way, you can keep both the profiles in use .But, if a property is common in both the bootstrap files, then program will pick the property from that bootstrap file whose profile matches with the runtime args : -Dspring.profiles.active