SpringBoot (8)总结

240 阅读1分钟

SpringBoot简化了Spring应用初始搭建以及开发过程,内置Tomcat、JUnit、Spring Boot Test等框架,与其他框架的整合也无需写xml了,它的本质是简化配置,没有对原有功能增强。

自动装配以及框架整合等在前面的文章都有说明,本篇主要对run()方法的流程做一个简要的描述,主要以链接的形式展示之前的内容。

实例化

通过new或者构建者模式实例化SpringApplication对象,在启动前可以设置很多参数,详情

启动

第一步 监听任务

StopWatch stopWatch = new StopWatch():任务id设置成空

stopWatch.start():任务名设置成空,开始计时

第二步 运行监听器类SpringApplicationRunListeners

SpringApplicationRunListeners listeners = getRunListeners(args):从配置文件spring.factories中得到指定org.springframework.boot.SpringApplicationRunListener的属性,此处是EventPublishingRunListener,将监听器集合赋予

listeners.starting()

第三步 环境类ConfigurableEnvironment

ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments):环境准备,加载配置文件,返回StandardServletEnvironment,详情

第四步 打印Banner

Banner printedBanner = printBanner(environment)详情

第五步 创建应用上下文并刷新

context = createApplicationContext():创建应用上下文

prepareContext(context, environment, listeners, applicationArguments,printedBanner):刷新前准备工作,详情

refreshContext(context):刷新上下文,这是最重要的一步,大约95%的工作在这一步完成,详情

afterRefresh(context, applicationArguments)

第六步 打印耗时

stopWatch.stop():停止计时

打印耗时

    if (this.logStartupInfo) {
        new StartupInfoLogger(this.mainApplicationClass)
                .logStarted(getApplicationLog(), stopWatch);
    }

第七步 启动成功

listeners.started(context)

listeners.running(context)

若启动失败则关闭

handleRunFailure(context, ex, exceptionReporters, null)详情

至此,SpringBoot启动完成