@ManagedResource public class RefreshScope extends GenericScope implements org.springframework.context.ApplicationContextAware
A Scope implementation that allows for beans to be refreshed dynamically at runtime (see refresh(String) and
refreshAll()). If a bean is refreshed then the next time the bean is accessed (i.e. a method is executed) a
new instance is created. All lifecycle methods are applied to the bean instances, so any destruction callbacks that
were registered in the bean factory are called when it is refreshed, and then the initialization callbacks are
invoked as normal when the new instance is created. A new bean instance is created from the original bean definition,
so any externalized content (property placeholders or expressions in string literals) is re-evaluated when it is
created.
Note that all beans in this scope are only initialized when first accessed, so the scope forces lazy
initialization semantics. The implementation involves creating a proxy for every bean in the scope, so there is a
flag proxyTargetClass which controls the proxy creation, defaulting to JDK
dynamic proxies and therefore only exposing the interfaces implemented by a bean. If callers need access to other
methods then the flag needs to be set (and CGLib present on the classpath). Because this scope automatically proxies
all its beans, there is no need to add <aop:auto-proxy/> to any bean definitions.
The scoped proxy approach adopted here has a side benefit that bean instances are automatically Serializable,
and can be sent across the wire as long as the receiver has an identical application context on the other side. To
ensure that the two contexts agree that they are identical they have to have the same serialization id. One will be
generated automatically by default from the bean names, so two contexts with the same bean names are by default able
to exchange beans by name. If you need to override the default id then provide an explicit id
when the Scope is declared.
SCOPED_TARGET_PREFIX| Constructor and Description |
|---|
RefreshScope()
Create a scope instance and give it the default name: "refresh".
|
| Modifier and Type | Method and Description |
|---|---|
void |
refresh(String name) |
void |
refreshAll() |
void |
setApplicationContext(org.springframework.context.ApplicationContext context) |
destroy, destroy, get, getConversationId, postProcessBeanFactory, registerDestructionCallback, remove, resolveContextualObject, setBeanLifecycleManager, setId, setName, setProxyTargetClass, setScopeCachepublic RefreshScope()
@ManagedOperation(description="Dispose of the current instance of bean name provided and force a refresh on next method execution.") public void refresh(String name)
@ManagedOperation(description="Dispose of the current instance of all beans in this scope and force a refresh on next method execution.") public void refreshAll()
public void setApplicationContext(org.springframework.context.ApplicationContext context)
throws org.springframework.beans.BeansException
setApplicationContext in interface org.springframework.context.ApplicationContextAwareorg.springframework.beans.BeansExceptionCopyright © 2015 Pivotal Software, Inc.. All rights reserved.