1

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?

AmitB10
  • 425
  • 6
  • 16
  • 2
    If you need to change your configuration at runtime you should check the spring config server: https://spring.io/guides/gs/centralized-configuration/. Of course you can do in a simple way by polling the file and reload the configuration if the file is changed. Depends on how is complicated your requirement. – Mario Santini Feb 10 '18 at 10:16
  • @MarioSantini why not make an answer out of the comment? – Kayaman Feb 10 '18 at 11:06
  • @Kayaman as there was no other answers yet I posted mine. – Mario Santini Feb 10 '18 at 15:14

1 Answers1

2

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.

Mario Santini
  • 2,905
  • 2
  • 20
  • 27
  • hello mario. for this point ==> A better way should be to have a config class that handle the configuration for your application. can you please provide an example or tutorial link ? – Parth Doshi May 25 '19 at 13:37
  • Sorry for the late answer, it depends on your specific case. If you have a trivial application, then you can opt for implementing your own config class. For a better solution, you should have a look to the Spring Config Server. – Mario Santini May 29 '19 at 17:24
  • Going for a cloud option like Spring Config server (which is the recommended solution) would be difficult in our case since the solution is deployed on premise and won't be exposed to open Internet. Rather, can you please provide a sample of how to implement own configuration class (singleton) ? – Parth Doshi May 31 '19 at 03:18
  • Hi, sorry I would have provide a simple example here, but in this days I'm very busy and I never found the time to write it, sorry. – Mario Santini Jun 04 '19 at 06:58
  • As your question is connected to Spring Boot I found this solution that maybe can help: https://stackoverflow.com/questions/40287771/how-to-reload-a-value-property-from-application-properties-in-spring/40288822#40288822 – Mario Santini Jun 08 '19 at 09:49