美文网首页
RefreshScope源码分析

RefreshScope源码分析

作者: 梦想又照进现实 | 来源:发表于2020-01-10 11:10 被阅读0次

RefreshScope核心对象

一、 属性
eager 默认为true,会进行refresh范围内的全部bean进行提前实例化

二、 方法
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) {
        if (!name.startsWith(SCOPED_TARGET_PREFIX)) {
            // User wants to refresh the bean with this name but that isn't the one in the
            // cache...
            name = SCOPED_TARGET_PREFIX + name;
        }
        // Ensure lifecycle is finished if bean was disposable
        if (super.destroy(name)) {
            this.context.publishEvent(new RefreshScopeRefreshedEvent(name));
            return true;
        }
        return false;
    }

发送RefreshScopeRefreshedEvent事件;
EventListener监听方法

@EventListener
    public void start(ContextRefreshedEvent event) {
        if (event.getApplicationContext() == this.context && this.eager
                && this.registry != null) {
            eagerlyInitialize();
        }
    }

重新创建Bean对象;

GenericScope的Bean处理

这个是核心的Bean处理类,主要定义了Bean的生命周期信息,Bean创建的工厂信息,以及Bean信息定义的缓存,除外还定义了Bean实例化前的动态修改bean信息的方法,实现当监听到配置信息更新事件后能够从缓存获取Bean的定义信息,对需要重新修改bean定义的进行动态销毁和重建;

主要的对象信息:

BeanLifecycleWrapper
包装bean生命周期的包装类,主要持有了ObjectFactory,利用其进行Bean对象的创建;

BeanLifecycleWrapperCache
BeanLifecycleWrapper的缓存,提高bean信息定义的获取效率;

*BeanFactoryPostProcessor *
BeanFactoryPostProcessor的主体是BeanFactory,并且该接口中只定义了一个方法,其将会在ApplicationContext内部的BeanFactory加载完bean的定义后,但是在对应的bean实例化之前进行回调。所以通常我们可以通过实现该接口来对实例化之前的bean定义进行修改。

BeanFactoryPostProcessorBean(工厂的后置处理器)
在bean真正实例化前,通过注入的BeanFactory对上下文动态修改

BeanDefinitionRegistryPostProcessor(Bean注册器的后置处理器)
更早于BeanFactoryPostProcessor动态处理上下文中的BeanDefinition信息

相关文章

网友评论

      本文标题:RefreshScope源码分析

      本文链接:https://www.haomeiwen.com/subject/zpepactx.html