SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:
- A /config subdirectory of the current directory. - The current directory - A classpath /config package - The classpath root
The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files
Question: when running a war
file on a tomcat
server: how can I add an additional location for the application.properties
outside the classpath or the tomcat container, like d:\application.properties
?
The custom location should get highest precedence regarding the locations above.
Problem is: I could of course add a /config
folder inside my exploded war in the tomcat webapps
folder, but then I'd lose any custom configuration if the webapps folder is cleaned and war is redeployed.
Thus I'd like to add an additional location outside.
Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration.
properties outside the classpath or the tomcat container, like d:\application.
You will need to add the application. properties file in your classpath. If you are using Maven or Gradle, you can just put the file under src/main/resources . If you are not using Maven or any other build tools, put that under your src folder and you should be fine.
For me the easiest way to do it, was to place a context file inside Tomcat's config folder. For example if your application is running under root path (eg. http://your_domain.com/
) you need to create a file [path_to_your_tomcat]/conf/Catalina/localhost/ROOT.xml
. If your application runs in a different path, for example http://your_domain.com/example_path
the file should be named like this [path_to_your_tomcat]/conf/Catalina/localhost/example_path.xml
. Inside this file you can specify a path to the external application.properties file that can be placed anywhere on your hard drive.
<?xml version="1.0" encoding="UTF-8"?> <Context> <Environment name="spring.config.location" value="file:/path/to/your/application/properties/file/" type="java.lang.String"/> </Context>
You can set a spring_config_location
environment variable pointing to the folder that contains your application.properties file.
In the case of Tomcat you can do this by adding the following line to your <TOMCAT_HOME>/bin/setenv.sh
file (create the file if missing):
export spring_config_location=/usr/local/tomcat/conf/
Place the properties file in that folder. In case you have multiple apps you can set the name of the properties file of each app to be unique. For a Spring Boot App I have done it like this:
@SpringBootApplication public class MyApplication { public static void main(String[] args) { System.setProperty("spring.config.name", "my-app"); SpringApplication.run(MyApplication.class, args); } }
This will pick the new name when run with BOOT. To have the name configured when deployed on Tomcat too, overwrite configure of SpringBootServletInitializer
like so:
public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(MyApplication.class).properties("spring.config.name: my-app"); } }
Then name your properties file like: my-app.properties
. Instead of the default name Spring will look for that. You can put all your apps properties files in the specified folder, /usr/local/tomcat/conf/
in our sample. Your external properties will get precedence. See here for priorities: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
UPDATE
Since Spring Boot 2 the behavior of spring_config_location
has changed (from the migration guide):
it previously added a location to the list of default ones, now it replaces the default locations. If you were relying on the way it was handled previously, you should now use spring.config.additional-location instead.
So based on your use case you should consider which of the properties to set as an environment variable. The new one should look like spring_config_additional-location
in setenv.sh
. Where files are looked up from is described in the reference documentation too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With