4

In my Spring application I load application.properties file from outside the application e.g. /user/home/properties/application.properties. The values in the file are injected via @Value annotation in the beans. The new requirement I've is to be able to change values in application.properties file and reload (or reinject) the new values in the beans.

Is something like this possible in Spring 3.2?

jsf
  • 2,851
  • 9
  • 30
  • 33
  • 1
    Take a look at this: http://stackoverflow.com/questions/13248066/how-to-reload-properties-with-spring. Once Spring has loaded the beans, I don't know if it can go modify them or replace them. – Sotirios Delimanolis Mar 07 '13 at 15:09
  • you should use refershScope: https://static.javadoc.io/org.springframework.cloud/spring-cloud-commons-parent/1.1.4.RELEASE/org/springframework/cloud/context/scope/refresh/RefreshScope.html – asfmlr Sep 23 '19 at 19:51
  • If you use `@Configuration Properties`, you should at least be able to do it "manually", ie use the setters for the fields read from file. – daniu May 18 '21 at 18:06

2 Answers2

0

On a standalone spring application in the main class you can do something like this:

 //load the appcontext with refresh value as false
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                    new String[] { "classpath:appcontext.xml" }, false);
//add the props file
context.getEnvironment().getPropertySources().addFirst(new ResourcePropertySource("classpath:app.properties"));
//refresh the context
context.refresh();

What this does is to load the spring context with the properties defined in the all the properties which is called inside the appcontext.xml file, but does not refresh at load time. Then it says to load the app.properties as first. At that time only the values in app.properties is considered. And then the context is refreshed. Now the property values in app.properties file is loaded. With this you do not need to rebuild the application, you can just change the values and restart the application

Dhanush Gopinath
  • 5,652
  • 6
  • 37
  • 68
  • 1
    The problem with this approach is I'll have to restart the application everytime I make a change in one of my application.properties file. The solution I'm looking for is - to be able to refresh/reload new values from application.properties file at runtime without have to restart the application. – jsf Mar 07 '13 at 15:45
  • 1
    This might help then - http://www.morgan-design.com/2012/08/reloadable-application-properties-with.html – Dhanush Gopinath Mar 07 '13 at 15:53
0

You can't reload the properties in application.properties once the application context has started unless the java process has closed and you need to build and run again. Ideal option is to use a session or cache or any event driven messaging framework like Kafka. It's totally depend on your requirement and frameworks that you are capable to use.

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
K M Dilshan Udara
  • 1,025
  • 1
  • 11
  • 15