• SpringBoot 源码 | refreshContext 方法解析


    SpringBoot 源码 | refreshContext 方法解析

    SpringBoot

    在 SpringBoot 启动流程中,主要的两个阶段是初始化 SpringApplication 对象以及 SpringApplication.run 方法执行的内容,今天主要细讲的是 SpringApplication.run 中的刷新容器 refreshContext 方法,refreshContext 的源码如下

    refreshContext 源码

    refreshContext 方法主要是刷新容器,下面我们来看一下 refreshContext 的源码,点击 SpringApplication.run 方法的 refreshContext 方法

    看到 refreshContext 的方法内容很简单,主要是调用 refresh 方法

    继续点击 refresh 方法可以看到,Refresh the underlying {@link ApplicationContext}也就是刷新底层的 ApplicationContext

    继续跟进去,这里要选择 AbstractApplicationContext

    这里我们看一下 AbstractApplicationContext 的注释,注释内容

    Abstract implementation of the {@link org.springframework.context.ApplicationContext} interface. Doesn’t mandate the type of storage used for configuration; simply implements common context functionality. Uses the Template Method design pattern,requiring concrete subclasses to implement abstract methods.翻译过来就是当前抽象类是 ApplicationContext 接口的抽象实现,不强制要求用于配置的存储类型;它只是实现了公共上下文功能,使用的是模板方法的设计模式,需要具体的子类来实现抽象方法。下面我们再来看 refresh 方法

    refresh 方法

    refresh 方法主要是刷新应用程序上下文,这里主要涉及到准备刷新上下文,调用上下文注册为 bean 的工厂处理器,初始化上下文的消息源,初始化特定上下文子类中的其他特殊 bean,检查监听器 bean 并注册,最后发布相应的事件并销毁已经创建的单例及重置 active 标志,整体的注解我都直接加在源码中了

    @Override
    public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
    
            // Prepare this context for refreshing. 准备刷新上下文
            prepareRefresh();
    
            // Tell the subclass to refresh the internal bean factory. 告诉子类刷新内部bean工厂
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    
            // Prepare the bean factory for use in this context. 准备bean工厂以用于此上下文
            prepareBeanFactory(beanFactory);
    
            try {
                // Allows post-processing of the bean factory in context subclasses. 允许在上下文子类中对bean工厂进行后置处理
                postProcessBeanFactory(beanFactory);
    
                StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
                // Invoke factory processors registered as beans in the context. 调用上下文中注册为bean的工厂处理器
                invokeBeanFactoryPostProcessors(beanFactory);
    
                // Register bean processors that intercept bean creation.  注册拦截器bean创建的bean处理器
                registerBeanPostProcessors(beanFactory);
                beanPostProcess.end();
    
                // Initialize message source for this context. 初始化此上下文的消息源
                initMessageSource();
    
                // Initialize event multicaster for this context. 为此上下文初始化事件多播
                initApplicationEventMulticaster();
    
                // Initialize other special beans in specific context subclasses. 初始化特定上下文子类中的其他特殊bean
                onRefresh();
    
                // Check for listener beans and register them. 检查监听器bean并注册
                registerListeners();
    
                // Instantiate all remaining (non-lazy-init) singletons. 实例化所有剩余的(非懒惰初始化)单例
                finishBeanFactoryInitialization(beanFactory);
    
                // Last step: publish corresponding event. 最后一步:发布相应的事件
                finishRefresh();
            }
    
            catch (BeansException ex) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Exception encountered during context initialization - " +
                            "cancelling refresh attempt: " + ex);
                }
    
                // Destroy already created singletons to avoid dangling resources. 销毁已创建的单例以避免悬空资源
                destroyBeans();
    
                // Reset 'active' flag. 重置active标志
                cancelRefresh(ex);
    
                // Propagate exception to caller. 将异常传播到调用方
                throw ex;
            }
    
            finally {
                // Reset common introspection caches in Spring's core, since we
                // might not ever need metadata for singleton beans anymore...
                //重置Spring核心中的公共内省缓存,因为我们可能不再需要单例bean的元数据
                resetCommonCaches();
                contextRefresh.end();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    到这里 SpringBoot 启动流程中的 refreshContext 方法的主体源码内容已经讲解完了,后续我们继续深入来看 refresh 方法内部子方法的源码,敬请关注。

  • 相关阅读:
    【C++之类型转换】static_cast、dynamic_cast、const_cast、reinterpret_cast用途与限制
    阿里云邮件推送配置教程:有哪些关键步骤?
    Unity Hub报错:No valid Unity Editor license found. Please activate your license.
    阿里国际、ebay测评自养号,如何看待自己产品的销量?
    23个优秀开源免费BI仪表盘
    UE4 回合游戏项目 23- 进入战斗
    【QT】QTableView或QTableWidget 搭配QLineEdit实现数据的搜索显示
    存档&改造【01】模板导入&租户登录
    HTML中 &# 的作用
    ZCMU操作系统期末复习策略
  • 原文地址:https://blog.csdn.net/csdn565973850/article/details/126885962