What is RefreshScope ?
RefreshScope(org.springframework.cloud.context.scope.refresh)是spring cloud提供的一种特殊的scope实现,用来实现配置、实例热加载。 RefreshScope继承关系如下所示
![](https://img2020.cnblogs.com/blog/1368530/202010/1368530-20201013104205043-1650840952.png)
Scope -> GenericScope -> RefreshScope,Scope是Spring2.0开始有的概念,主要是用于定义spring的注入范围。GenericScope 是 一个通用Scope实现。
2. RefreshScope实现原理
要想了解RefreshScope的实现原理,首先要了解Scope在Springboot生命周期中涉及到的功能点
2.1 Scope在创建Bean实例
AbstractBeanFactory#doGetBean
此方法返回一个Bean对象
/* * Return an instance, which may be shared or independent, of the specified bean. */ protected <T> T doGetBean(...) { final String beanName = transformedBeanName(name); Object bean; // Eagerly check singleton cache for manually registered singletons. Object sharedInstance = getSingleton(beanName); if (sharedInstance != null && args == null) { ... bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); } }else { // Fail if we're already creating this bean instance: // We're assumably within a circular reference. ... // Check if bean definition exists in this factory. ... // Guarantee initialization of beans that the current bean depends on. .... //Create bean instance. if (mbd.isSingleton()) { ... } else if (mbd.isPrototype()) ... { String scopeName = mbd.getScope(); final Scope scope = this.scopes.get(scopeName); Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() {... }); ... } ... }