How to externalize application.properties in a Tomcat web server for Spring?

SpringApplication will load properties from application.properties files in the following places 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 sorted by priority (properties defined in the locations above in the list override those defined in lower places).

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 the war file on the tomcat : how to add additional space for application.properties outside the tomcat class or container, for example d:\application.properties

The user location should have the highest priority with respect to the locations above.

The problem is this: I could, of course, add the /config folder inside my exploded war to the tomcat webapps folder, but then I will lose any custom configuration if the webapps folder is cleared and the war is redistributed.

So I would like to add extra space outside.

+13
java spring spring-boot tomcat
source share
6 answers

You can set spring_config_location environment spring_config_location point to the folder where the application.properties file is located.

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 it is missing):

 export spring_config_location=/usr/local/tomcat/conf/ 

Place the properties file in this folder. If you have multiple applications, you can specify the name of the properties file for each application so that it is unique. For the Spring Boot application, I did 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 select a new name when starting from BOOT. To configure the name when deploying to Tomcat, overwrite the SpringBootServletInitializer configuration as follows:

 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 as: my-app.properties . Instead of the default name, Spring will look for it. You can put all the properties files of your applications in the specified folder, /usr/local/tomcat/conf/ in our example. Your external properties will take priority. See the priorities here: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

UPDATE

Starting with Spring Boot 2, the behavior of spring_config_location has changed (from the migration guide ):

Previously, he added the location to the default list, now he replaces the default location. If you relied on how this was handled earlier, you should use spring.config.additional-location instead.

Therefore, 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 to look for files is also described in the help documentation .

+11
source share

For me, the easiest way to do this is to place the context file inside the Tomcat configuration folder. For example, if your application runs under the root path (for example, http://your_domain.com/ ), you need to create the 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 as [path_to_your_tomcat]/conf/Catalina/localhost/example_path.xml . Inside this file, you can specify the path to the external application.properties file, which 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> 
+9
source share

I had to do this several times, and the best approach I found was to configure the external directory as a resource of the class path in the container:

Tomcat class path

Then, in the directory, place the resources that you want to allocate, and everything will work fine. To load a resource in spring, you can do this:

 <beans:bean id="externalProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <beans:property name="location" value="classpath:[my-application-name]/applicationProperties.properties" /> <beans:property name="placeholderPrefix" value="!applicationProperties{" /> <beans:property name="placeholderSuffix" value="}" /> </beans:bean> 

You can see that, as you said, you can have several applications deployed in each tomcat, you can simply create a directory structure in the folder set in the classpath to support different application.properties for each of your war applications

enter image description here

If you want to dynamically maintain the application name section in the Spring configuration, you can do this in several ways during the packaging phase in maven or even using the application context path

+1
source share

Finally, I added the following property for externalization, for example, protected properties:

spring.config.additional-location=/etc/tomcat/<appname>/application-production.properties

0
source share

If someone is looking for a Linux solution, this works for us:
edit tomcat startup.sh
add:

  export spring_config_location=/<YOUR_PATH>/application.properties 

example:

  export spring_config_location=/app/conf/application.properties 
0
source share

for Tomcat 9 on Ubuntu 18.04 and spring boot 2, create the setenv.sh file in the $ CATALINA_HOME / bin / directory that works for me:

 #!/bin/bash export spring_config_additional_location="/opt/tomcat/latest/conf/application.properties" 

do not forget to set the file resolution if necessary

0
source share

All Articles