Externalise Properties in Spring Boot Application
Overview
Anything that varies among the deployment environments is considered a configuration. Which usually include:
- Database credentials
- Host address of dependent services
- Retry and timeouts
- Concurrency factor
- Logging levels
- and application-specific properties
For java based applications, configurations are kept in the .properties file in the form of key-value pairs. We should never hard code any configuration values in the codebase, which violates the 12 Factor app principle. Configuration should be supplied by the env in which the application is deployed.
Introduction to Externalize Properties
Externalize properties are nothing but configuration properties outside of the application jar.
Spring Boot facilitates the externalized configuration to work with the same application code in different environments. We can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration.
How to Externalize Spring Boot application.properties
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(Resource folder)
The higher item in the list overrides the lower one. For example, if we have the same property in the /config subdir and classpath root, then the spring framework will use the property located under the /config directory.
Let's verify it with an example:
Case 1 - application.properties from classpath
- We will keep one application.properties in the project classpath, i.e., under the resource folder.
- Create a spring bean to get the value and print it on the console.
- Call the method TestPropertyLoadingBean#printValue from ApplicationRunner#run method.
- Verify the output - Run the application and verify the output.
Case 2 application.properties in /config subdirectory
- Build the application using mvn clean install to create a bootable jar, which should get created in the target directory.
- Copy the bootable jar to a folder of your choice.
- Create a folder and name it config
- Place an application.properties inside the config folder with the below content.
The structure should look like this:
Now we have two properties files, one inside the bootable jar and another in the config subdirectory.
- Run the application and verify the output.
As expected, it loaded the value from the config directory and ignored the one inside the classpath.
Passing Specific Directory for Reading Properties Files
Though spring boot offers multiple predefined locations where application.properties can be placed for externalization.
For some applications, using the framework-defined location may not be possible. In that case, the application can use its location and tell the spring framework where to load the configuration from using the environment property spring.config.location.
Let's see the effect with the same example application.
- Place application.properties in any folder based on your operating system with the below content.
- Run the application with the argument --spring.config.location, specifying the folder where properties files are kept.
- Verify the output.
As expected, configuration from the custom location is used.
Conclusion
In this article, we covered the following:
- Importance of externalizing configuration properties
- Order in which spring look for configuration properties.
- Providing a custom location in case the framework-provided location is not a viable option.