The following is a bit strange, but it works. You create a custom scope called reconfigurable
that discards all the beans created in that scope whenever a configuration update occurs. Thus, after changing the configuration, a new bean will be created.
Actual configuration values must be obtained using the spring expression language, since the values for both the $ {} syntax and the PropertyOverrideConfigurer property appear to be constantly committed to BeanDefinition. A bean declaration for a bean with an override property of someProperty
as follows:
<bean class="blablu.Testbean" scope="reconfigurable" p:someProperty="#{ config['configexplicit']}"> <aop:scoped-proxy /> </bean>
You need to use aop: scoped-proxy so that the beans that use this bean always retrieve the latest configured bean from the custom scope.
Declaring properties using @Value
also works; if you use component scanning, you need to declare the area using annotation
@Scope(value="reconfigurableScope", proxyMode=ScopedProxyMode.TARGET_CLASS)
If you need details: the main idea of the area:
public class ReconfigurableScope implements Scope { private final Map<String, Object> nameToObjectMap = new ConcurrentHashMap<String, Object>(); public Object get(final String name, final ObjectFactory<?> objectFactory) { Object bean = nameToObjectMap.get(name); if (null == bean) { bean = objectFactory.getObject(); nameToObjectMap.put(name, bean); } return bean; }
In addition, some security and thread cleaning tools: remote beans need to be destroyed a little later and close the application context.
source share