Class RefreshScope
- java.lang.Object
-
- org.springframework.cloud.context.scope.GenericScope
-
- org.springframework.cloud.context.scope.refresh.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
@ManagedResource public class RefreshScope extends GenericScope implements 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)andrefreshAll()). 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 explicitidwhen the Scope is declared.- Since:
- 3.1
- Author:
- Dave Syer
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.springframework.cloud.context.scope.GenericScope
GenericScope.LockedScopedProxyFactoryBean<S extends GenericScope>
-
-
Constructor Summary
Constructors Constructor Description RefreshScope()Creates a scope instance and gives it the default name: "refresh".
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intgetOrder()voidonApplicationEvent(org.springframework.context.event.ContextRefreshedEvent event)voidpostProcessBeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry registry)booleanrefresh(Class type)WARNING: This method refreshes beans from any context in the hierarchy using the main application context.booleanrefresh(String name)voidrefreshAll()voidsetApplicationContext(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 org.springframework.cloud.context.scope.GenericScope
destroy, destroy, get, getConversationId, getErrors, getLock, getName, postProcessBeanFactory, registerDestructionCallback, remove, resolveContextualObject, setId, setName, setScopeCache
-
-
-
-
Method Detail
-
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
public boolean refresh(Class type)
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
-
-