@ManagedResource public class RefreshScope extends GenericScope implements org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor, org.springframework.core.Ordered
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 |
---|---|
int |
getOrder() |
void |
postProcessBeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry registry) |
boolean |
refresh(String name) |
void |
refreshAll() |
void |
setApplicationContext(org.springframework.context.ApplicationContext context) |
void |
setEager(boolean eager)
Flag to determine whether all beans in refresh scope should be instantiated eagerly
on startup.
|
void |
setOrder(int order) |
void |
start(org.springframework.context.event.ContextRefreshedEvent event) |
destroy, destroy, get, getConversationId, getErrors, getName, postProcessBeanFactory, registerDestructionCallback, remove, resolveContextualObject, setBeanLifecycleManager, setId, setName, setProxyTargetClass, setScopeCache
public RefreshScope()
public int getOrder()
getOrder
in interface org.springframework.core.Ordered
public void setOrder(int order)
public void setEager(boolean eager)
eager
- the flag to setpublic void postProcessBeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry registry) throws org.springframework.beans.BeansException
postProcessBeanDefinitionRegistry
in interface org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor
org.springframework.beans.BeansException
@EventListener public void start(org.springframework.context.event.ContextRefreshedEvent event)
@ManagedOperation(description="Dispose of the current instance of bean name provided and force a refresh on next method execution.") public boolean 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.ApplicationContextAware
org.springframework.beans.BeansException
Copyright © 2016 Pivotal Software, Inc.. All rights reserved.