Consider the following scenario. I have a Spring application context with a bean whose properties should be configurable, think DataSource
or MailSender
You should have a look at JMX. Spring also provides support for this.
Option 1 :
configurable
bean into the DataSource
or MailSender
. Always get the configurable values from the configuration bean from within these beans.configurable
bean run a thread to read the externally configurable properties (file etc..) periodically. This way the configurable
bean will refresh itself after the admin had changed the properties and so the DataSource
will get the updated values automatically.
Option 2 (bad, i think, but maybe not - depends on use case) :
DataSource
/ MailSender
- using prototype
scope. In the init of the bean, read the properties afresh.Option 3 : I think, @mR_fr0g suggestion on using JMX might not be a bad idea. What you could do is :
HTH!
You may want to have a look at the Spring Inspector a plug-gable component that provides programmatic access to any Spring based application at run-time. You can use Javascript to change configurations or manage the application behaviour at run-time.
This is not something I tried, I am trying to provide pointers.
Assuming your application context is a subclass of AbstractRefreshableApplicationContext(example XmlWebApplicationContext, ClassPathXmlApplicationContext). AbstractRefreshableApplicationContext.getBeanFactory() will give you instance of ConfigurableListableBeanFactory. Check if it is instance of BeanDefinitionRegistry. If so you can call 'registerBeanDefinition' method. This approach will be tightly coupled with Spring implementation,
Check the code of AbstractRefreshableApplicationContext and DefaultListableBeanFactory(this is the implementation you get when you call 'AbstractRefreshableApplicationContext getBeanFactory()')
Here is the nice idea of writing your own PlaceholderConfigurer that tracks the usage of properties and changes them whenever a configuration change occurs. This has two disadvantages, though:
I can think of a 'holder bean' approach (essentially a decorator), where the holder bean delegates to holdee, and it's the holder bean which is injected as a dependency into other beans. Nobody else has a reference to holdee but the holder. Now, when the holder bean's config is changed, it recreates the holdee with this new config and starts delegating to it.