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

    @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) 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
    • Constructor Detail

      • RefreshScope

        public RefreshScope()
        Creates a scope instance and gives it the default name: "refresh".
    • Method Detail

      • getOrder

        public int getOrder()
        Specified by:
        getOrder in interface org.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:
        postProcessBeanDefinitionRegistry in interface org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor
        Overrides:
        postProcessBeanDefinitionRegistry in class GenericScope
        Throws:
        org.springframework.beans.BeansException
      • onApplicationEvent

        public void onApplicationEvent​(org.springframework.context.event.ContextRefreshedEvent event)
        Specified by:
        onApplicationEvent in interface org.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:
        setApplicationContext in interface org.springframework.context.ApplicationContextAware
        Throws:
        org.springframework.beans.BeansException