0

I would like to create a Spring Boot app (packaged as WAR) that automatically override some configuration file depending on the environment where it is deployed. I want also that the property file is external to the WAR. I'm using as OS Centos and Tomcat as web server.

I'm trying to follow the response of Vladimir Mitev in the following question Similar question.

In order to achieve this I have created this SPRING_CONFIG_ADDITIONAL_LOCATION environment variable. Then in the path I have created a db.properties file containing the property that I would like to override.

Then in the servlet initializer I put this configuration:

public class ServletInitializer extends SpringBootServletInitializer {
  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(MyApplication.class).properties("spring.config.name: db");

  }
}

This is the other needed class:

@SpringBootApplication
public class MyApplication{
    public static void main(String[] args) {
        System.setProperty("spring.config.name", "db");
        SpringApplication.run(MyApplication.class, args);
    }
}

But Spring Boot during the initialization doesn't find the db.properties file.

In the official documentation seems that I have to use this "spring.config.additional-location" in order to achieve my goal but I don't figure out how.

Paolo
  • 555
  • 4
  • 28

2 Answers2

2

You can try:

  1. Create folder with name "config" in [tomcat-server-folder]/bin
  2. Create db.properties file in config folder. ([tomcat-server-folder]/bin/config/db.properties)
  3. Just create file setenv.sh in Tomcat's bin directory with content:

JAVA_OPTS="$JAVA_OPTS -Dspring.config.name=db"

Or you can specific location of config file:

JAVA_OPTS="$JAVA_OPTS -Dspring.config.location=/opt/app/default.properties,/opt/app/db.properties"

huytmb
  • 3,647
  • 3
  • 12
  • 18
1

it is too late to configure in the SpringBootServletInitializer, you must set the property before spring application run

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        System.setProperty("spring.config.name", "db");
        SpringApplication.run(MyApplication.class, args);
    }
}
clevertension
  • 6,929
  • 3
  • 28
  • 33
  • Sorry I forgot to post that part in the question, but I have it. And still doesn't work. Mu doubt is about this env variable SPRING_CONFIG_ADDITIONAL_LOCATION. I suppose that spring doesn't looks for this variable. – Paolo Apr 18 '19 at 09:06
  • please paste that how can you set the value of SPRING_CONFIG_ADDITIONAL_LOCATION – clevertension Apr 18 '19 at 09:12
  • in this file tomcat/bin/setenv.sh I have this simple line: export SPRING_CONFIG_ADDITIONAL_LOCATION=/usr/local/tomcat/conf/ – Paolo Apr 18 '19 at 09:21
  • There was a problem with a space in the setenv.sh file and the variable doesn't work. Thank you for your help! – Paolo Apr 18 '19 at 09:33