Class RefreshScope
- All Implemented Interfaces:
EventListener, org.springframework.beans.factory.Aware, org.springframework.beans.factory.config.BeanFactoryPostProcessor, org.springframework.beans.factory.config.Scope, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>, 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 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.
- Since:
- 3.1
- Author:
- Dave Syer
-
Nested Class Summary
Nested classes/interfaces inherited from class GenericScope
GenericScope.LockedScopedProxyFactoryBean<S> -
Field Summary
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Constructor Summary
ConstructorsConstructorDescriptionCreates a scope instance and gives it the default name: "refresh". -
Method Summary
Modifier and TypeMethodDescriptionintgetOrder()voidonApplicationEvent(org.springframework.context.event.ContextRefreshedEvent event) voidpostProcessBeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry registry) booleanWARNING: This method refreshes beans from any context in the hierarchy using the main application context.booleanvoidvoidsetApplicationContext(org.springframework.context.ApplicationContext context) voidsetEager(boolean eager) Flag to determine whether all beans in refresh scope should be instantiated eagerly on startup.voidsetOrder(int order) voidstart(org.springframework.context.event.ContextRefreshedEvent event) Methods inherited from class GenericScope
destroy, destroy, get, getConversationId, getErrors, getLock, getName, postProcessBeanFactory, registerDestructionCallback, remove, resolveContextualObject, setId, setName, setScopeCacheMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.context.ApplicationListener
supportsAsyncExecution
-
Constructor Details
-
RefreshScope
public RefreshScope()Creates a scope instance and gives it the default name: "refresh".
-
-
Method Details
-
getOrder
public int getOrder()- Specified by:
getOrderin interfaceorg.springframework.core.Ordered
-
setOrder
public void setOrder(int order) -
setEager
public void setEager(boolean eager) Flag to determine whether all beans in refresh scope should be instantiated eagerly on startup. Default true.- Parameters:
eager- The flag to set.
-
postProcessBeanDefinitionRegistry
public void postProcessBeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry registry) throws org.springframework.beans.BeansException - Specified by:
postProcessBeanDefinitionRegistryin interfaceorg.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor- Overrides:
postProcessBeanDefinitionRegistryin classGenericScope- Throws:
org.springframework.beans.BeansException
-
onApplicationEvent
public void onApplicationEvent(org.springframework.context.event.ContextRefreshedEvent event) - Specified by:
onApplicationEventin interfaceorg.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>
-
start
public void start(org.springframework.context.event.ContextRefreshedEvent event) -
refresh
WARNING: This method refreshes beans from any context in the hierarchy using the main application context.- Parameters:
type- bean type to rebind.- Returns:
- true, if successful.
-
refresh
@ManagedOperation(description="Dispose of the current instance of bean name provided and force a refresh on next method execution.") public boolean refresh(String name) -
refreshAll
@ManagedOperation(description="Dispose of the current instance of all beans in this scope and force a refresh on next method execution.") public void refreshAll() -
setApplicationContext
public void setApplicationContext(org.springframework.context.ApplicationContext context) throws org.springframework.beans.BeansException - Specified by:
setApplicationContextin interfaceorg.springframework.context.ApplicationContextAware- Throws:
org.springframework.beans.BeansException
-