AmitB10
AmitB10

Reputation: 423

Can we change the value of a key in application.properties file in spring boot?

I have one value in properties file lets say

my.flag=false

I am placing this file in {location of .war file}/config folder, to override the internal application.properties file. So when in my controller, I just print the value of this key, it does come out as false correctly.

Now my question is if I change this value in the application.properties to true, still in the controller I get the previous value(value at the time of starting the server).

So does it requires server restart each time if I have to change the value in application.properties file?

Upvotes: 1

Views: 5223

Answers (1)

Mario Santini
Mario Santini

Reputation: 3003

You don't need to restart your application.

The easiest way is to check the property file each time you are accessing a property. In that way your application will always be sync with the new values saved in file.

But is not efficient as you have to read a file every time you access a property.

A better way should be to have a config class that handle the configuration for your application.

All classes in your app will access the config class to read the values.

And the config class will polling the file on intervals to load new values.

The easiest way is to have the config class as a singleton.

Another solution provided from the Spring framework is the Config Server.

This is intended for the cloud, but if your app and your configuration are complicated and need more features you should check this possibility.

Upvotes: 2

Related Questions