前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot 源码 | refreshContext 方法解析

SpringBoot 源码 | refreshContext 方法解析

作者头像
六月的雨在Tencent
发布2024-03-29 09:07:01
1190
发布2024-03-29 09:07:01
举报
文章被收录于专栏:CSDN
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 标志,整体的注解我都直接加在源码中了

代码语言:javascript
复制
@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();
        }
    }
}

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-03-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringBoot 源码 | refreshContext 方法解析
  • SpringBoot
    • refreshContext 源码
      • refresh 方法
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档