0

I am looking to load properties from file On Demand in my spring boot application.

I have a requirement to load the properties from the file when there is a change in the properties file.

I am using @Configuration properties to load the properties, by setting the spring.config.location=file:/../test-properties.yml.

Bean class that holds the data

@Configuration

@ConfigurationProperties public class GetValues {

List<String> values = new ArrayList<String>();

//getters and setters }

test-properties.yml

values: - X1 - X2 - X3

I have a rest service to load the data loaded from properties file to backend, within which I am Autowiring the GetValues Bean.

Whenever there is a change in the property file, I will call this service which would load the properties to the Backend.

Right Now, It is loading the same properties everytime, as the properties are already loaded in to the context.

Is there a way to reload the properties from the file everytime I hit the service? I don't want to use the Spring-Cloud, @RefreshScope.

2 Answers 2

0

There are two solutions

  1. Using spring devtools you can achieve auto restart when config file change. Only desirable in development environment.

  2. Make that when you hit the service the properties file get loaded, and update your config class attributes. Abstract:

    Properties prop = new Properties();
    InputStream input = null;
    input = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileNameProperties);
    prop.load(input);
    bean.setValue(prop.getProperty("attribute1"));
    
0

If you don't want to use Spring Cloud Config / @RefreshScope, one option would be to the usage of a FileChangeListener / WatchService found in Apache commons (or similar library) or JDK 7 and reload the properties. Make sure you synchronize the usage of the loaded properties and updating them when the file changes, maybe through a ReentrantReadWriteLock.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.