Reputation: 2981
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?
Upvotes: 4
Views: 9384
Reputation: 1195
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.
Upvotes: 0
Reputation: 5739
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
Upvotes: 0