SpringBoot, how to use external reloadable properties file

In this article I will show how to use a property file outside the war create by spring boot. The application will also consider every change made on this file without restart the application.

Allan Santos
3 min readMay 20, 2019

If you wanna check the code, you can download it from git.

When we start a new spring boot application we get the file application.properties under the resources directory. This file can be use for configure all kind of stuff, endpoints of yours services, database connection, messages in general, log and so on.

Struct of an empty application

Usually, we use the profile resource to config a file for each environment. For instance, you can have application.properties (used at local environment), application-tst.properties (used at test environment) and so on.

For this approach to work, is needed configure spring.profiles.active as a JVM args or OS variable in each environment existent, because when app start running, the application will use file configuration based on that information.

Here, we’ll see another approach. Won’t be application.properties inside the jar or war, instead will be an application property file in each environment.

It is important to highlight, that is possible to have an hybrid approach. Here part of configurations properties are inside the application and part of them are outside. I prefer using an hybrid approach, but for keep the things simpler, this article shows only how use all of the properties outside the application.

Application sample

The magic happens because the classes ConfigResource class and PropertySourceConfig class. Let’s take a look one them.

When the app starts, the class ConfigResource class will by found by spring scan and after that, the application will register our PropertySourceConfig class as responsible for configure the application properties.

The class PropertySourceConfig has two constants as showed bellow:

private static final String APP_CONFIG_NAME = “application-external.properties”;private static final String APP_CONFIG_PATH = Optional.ofNullable(System.getProperty(“CONFIG_DIR”).orElseThrow(() -> new RuntimeException(“CONFIG_DIR must be config.”));

The constant APP_CONFIG_PATH is load by JVM arg (like -DCONFIG_DIR), if this variable not exists application will fail to initialize.

The class PropertySourceConfig implements EnvironmentAware and because of that, is possible use that method setEnvironment to load the external property file.

Method configureWatcher, responsible for monitoring changes on property file

The method configureWatcher is called by the constructor method of PropertySourceConfig class.

This method configure a monitor to the property file. Each time the property file changes the method prepareProperties will be called.

method which load and reload property file

The method prepareProperties is the responsible for load and reload application properties based on application property file.

Simple rest controller for show

The class HelloController is a simple rest controller, just to be able to test the result after change properties file.

It is important to highlight, that is necessary use the annotation @Value with careful. The information loaded inside the message attribute will only be reload when HelloController class is recreated. In that particular case, it is necessary use @RequestScope for make that happens.

--

--