一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Spring Cloud动态配置刷新RefreshScope使用代码示例

时间:2022-08-29 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下Spring Cloud动态配置刷新RefreshScope使用代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

用过Spring Cloud的同学都知道在使用动态配置刷新的我们要配置一个@RefreshScope,在类上才可以实现对象属性的的动态更新。

@RefreshScope 能实现动态刷新全仰仗着@Scope这个注解。

一、了解@RefreshScope,先要了解@Scope

1、RefreshScope继承于GenericScope, 而GenericScope实现了Scope接口。

2、@Scope代表了Bean的作用域,我们来看下其中的属性:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Scope {
        /**
         * Alias for {@link #scopeName}.
         * @see #scopeName
         */
        @AliasFor("scopeName")
        String value() default "";
        /**
         *  singleton  表示该bean是单例的。(默认)
     *  prototype    表示该bean是多例的,即每次使用该bean时都会新建一个对象。
     *  request        在一次http请求中,一个bean对应一个实例。
     *  session        在一个httpSession中,一个bean对应一个实例
         */
        @AliasFor("value")
        String scopeName() default "";
        /**
    *   DEFAULT			不使用代理。(默认)
        * 	NO				不使用代理,等价于DEFAULT。
        * 	INTERFACES		使用基于接口的代理(jdk dynamic proxy)。
        * 	TARGET_CLASS	使用基于类的代理(cglib)。
    */
        ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;
}

3、@RefreshScope等同于scopeName="refresh"的@Scope:

 @Scope("refresh")
 public @interface RefreshScope {
     ...
 }

二、RefreshScope 的实现原理

1、@RefreshScope的实现

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Scope("refresh")
@Documented
public @interface RefreshScope {
        /**
         * @see Scope#proxyMode()
         */
        ScopedProxyMode proxyMode() default ScopedProxyMode.TARGET_CLASS;
}

可以看出它使用的就是@Scope,其内部就一个属性默认ScopedProxyMode.TARGET_CLASS。那我们来看下Scope这个接口:

public interface Scope {
        Object get(String name, ObjectFactory objectFactory);
        @Nullable
        Object remove(String name);
        void registerDestructionCallback(String name, Runnable callback);
        @Nullable
        Object resolveContextualObject(String key);
        @Nullable
        String getConversationId();
}

主要看看Object get(String name, ObjectFactory objectFactory)这个方法帮助我们来创建一个新的bean,也就是说 @RefreshScope在调用刷新的时候会使用get方法来给我们创建新的对象,这样就可以通过spring的装配机制将属性重新注入了,也就实现了所谓的动态刷新。

2、GenericScope帮我们实现了Scope最重要的get(String name, ObjectFactory objectFactory)方法,在GenericScope 里面 包装了一个内部类BeanLifecycleWrapperCache来对加了 @RefreshScope 从而创建的对象进行缓存,使其在不刷新时获取的都是同一个对象。

public class GenericScope implements Scope, BeanFactoryPostProcessor...{
      @Override
      public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
          throws BeansException {
          beanFactory.registerScope(this.name/*refresh*/, this/*RefreshScope*/);
          ...
      }
}

三、使用——@RefreshScope 使用流程

1、需要动态刷新的类标注@RefreshScope注解

2、@RefreshScope 注解标注了@Scope 注解,并默认了ScopedProxyMode.TARGET_CLASS; 属性,此属性的功能就是在创建一个代理,在每次调用的时候都用它来调用GenericScope get 方法来获取对象

3、如属性发生变更会调用 ContextRefresher refresh() -》RefreshScope refreshAll() 进行缓存清理方法调用,并发送刷新事件通知 -》 GenericScope 真正的 清理方法destroy() 实现清理缓存

4、在下一次使用对象的时候,会调用GenericScope get(String name, ObjectFactory objectFactory) 方法创建一个新的对象,并存入缓存中,此时新对象因为Spring 的装配机制就是新的属性了。

热门栏目