springboot启动流程源码分析(二)run(args)

本文深入剖析SpringBoot的启动流程,从获取监听器、构建环境、初始化上下文到刷新应用上下文,详细解读`run`方法的每一步。通过StopWatch记录运行时间,了解启动过程中的关键步骤,如环境准备、Banner打印、上下文刷新等。为后续的源码系列文章奠定基础。
摘要由CSDN通过智能技术生成

springboot源码分析系列文章

springboot启动流程源码之一(new SpringApplication(primarySources))

①整个run方法
在这里插入图片描述
②对整个Run方法进行注释说明

/**
   * Run the Spring application, creating and refreshing a new
   * {@link ApplicationContext}.
   * @param args the application arguments (usually passed from a Java main method)
   * @return a running {@link ApplicationContext}
   */
public ConfigurableApplicationContext run(String... args) {
    //记录程序运行时间
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    // ConfigurableApplicationContext Spring 的上下文
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    configureHeadlessProperty();
    //1、获取并启动监听器
    SpringApplicationRunListeners listeners = getRunListeners(args);
    listeners.starting();
    try {
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        //2、构造应用上下文环境
        ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
        //处理需要忽略的Bean
        configureIgnoreBeanInfo(environment);
        //打印banner
        Banner printedBanner = printBanner(environment);
        //3、初始化应用上下文
        context = createApplicationContext();
        //实例化SpringBootExceptionReporter.class,用来支持报告关于启动的错误
        exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
                                                         new Class[] { ConfigurableApplicationContext.class }, context);
        //4、刷新应用上下文前的准备阶段
        prepareContext(context, environment, listeners, applicationArguments, printedBanner);
        //5、刷新应用上下文
        refreshContext(context);
        //刷新应用上下文后的扩展接口
        afterRefresh(context, applicationArguments);
        //时间记录停止
        stopWatch.stop();
        if (this.logStartupInfo) {
            new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
        }
        //发布容器启动完成事件
        listeners.started(context);
        callRunners(context, applicationArguments);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, listeners);
        throw new IllegalStateException(ex);
    }try {
        listeners.running(context);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, null);
        throw new IllegalStateException(ex);
    }
    return context;
}

③拆解代码进行解读

StopWatch stopWatch = new StopWatch();
stopWatch.start();
---------------------------------------------------------------
StopWatch是位于org.springframework.util包下的一个工具类,
通过它可方便的对程序部分代码进行计时(ms级别),适用于同步单线程代码块。
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting()
---------------------------------------------------------------
创建所有 Spring 运行监听器并发布应用启动事件
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
---------------------------------------------------------------
此方法是准备运行时的环境
1、加载外部化配置的资源到environment;
2、触发ApplicationEnvironmentPreparedEvent事件
configureIgnoreBeanInfo(environment);
---------------------------------------------------------------
配置spring.beaninfo.ignore,并添加到名叫systemProperties的PropertySource中;默认为true即开启
Banner printedBanner = printBanner(environment);
---------------------------------------------------------------
打印banner
Banner printedBanner = printBanner(environment);
---------------------------------------------------------------
打印banner
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
 ---------------------------------------------------------------
 刷新应用上下文前的准备阶段
refreshContext(context)
---------------------------------------------------------------
(重点重点!!!)
刷新应用上下文
afterRefresh(context, applicationArguments);
---------------------------------------------------------------
刷新应用上下文后的扩展接口
stopWatch.stop();
---------------------------------------------------------------
时间记录停止
listeners.started(context);
---------------------------------------------------------------
发布容器启动完成事件

④整个run方法分为六步
第一步:获取并启动监听器
第二步:构造应用上下文环境
第三步:初始化应用上下文
第四步:刷新应用上下文前的准备阶段
第五步:刷新应用上下文
第六步:刷新应用上下文后的扩展接口

后续spring源码系列文章会对这些方法进行详细解读

关注公众号
在这里插入图片描述
每周会更新干货知识

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值