Monday, January 6, 2014

Spring Boot: External Config File for Container Deployed War



Spring Boot resolves external configuration file such as "application.properties" or "application.yml" in following order:

  1. classpath root
  2. current directory
  3. classpath /config package
  4. /config subdir of the current directory.
This is usually good for running spring boot application with executable jar (or war).
$ java -jar my-application.jar

However, when you convert your application to build a war file which gets deployed to a servlet container and want to keep the configuration file outside of the war file, the default search path doesn't work. 

One solution for this is that you can specify the external config file location in your servlet container's "spring.config.location" system property.


For example, in tomcat:

"CATALINA_HOME/bin/setenv.sh"
export CATALINA_OPTS="-Dspring.config.location=file:/usr/local/myapp/application.properties"

* The value of system property is evaluated as spring resource style format.
So, if you are specifying a file outside of classpath, you need to put "file:" prefix.






2 comments:

Anonymous said...

What happens if the container is shared by multiple spring boot based war files?

Daniel Mora said...

For an alternative solution, when the container is shared by multiple spring boot based war files, check this

http://stackoverflow.com/questions/31017064/how-to-externalize-spring-boot-application-properties-to-tomcat-lib-folder