Search code examples
javaspring-boottomcatconfigurationwar

Spring Boot 2.0 externalize property file in Tomcat using WAR packaging


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.


Solution

  • 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);
        }
    }