I would like to have the ability to periodically check some properties file and apply changes in my app accordingly.
In my java-spring application I'm checking periodically whether the properties file loaded from the file-system has changed:
Resource propsResource = new FileSystemResource("/path/to/properties/file");
File propertiesFile = propsResource.getFile();
long currentTimeStamp = propertiesFile.lastModified();
if (currentTimeStamp != lastModified) {
//File Changed
}
In case it has changed I would like to reload my spring beans since some of them are actually loaded conditionally relaying on the properties values in the properties file.
Using the refresh method did not do the trick so I'm actually calling the close method and recreating my context:
context.close();
context = new AnnotationConfigApplicationContext(AppConfig.class);
This does the trick. My problem is that I have some beans that contain cached data and I don't want to lose this data. One solution is to keep using this method but make sure my cached data resides in classes that are not managed by spring (either static or singletones managed by me).
Another solution could be to copy my cached data before recreating the context and then set it back to the newly create beans (those that hold the cached data) but this feels super ugly.
Is there a better approach for this?