SpringBoot源码学习(一) 启动流程解析

简介: ## 索引 + [SpringBoot源码学习(一) 启动流程解析](https://www.atatech.org/articles/145056) + [SpringBoot源码学习(二) 初始化环境,创建容器,初始化Failure Analyzers](https://www.atatech.org/articles/145181) + [SpringBoot源码学习(三) BeanF

索引

前言

  • 9012年了, java社区里面spring全家桶一直是支柱一般的存在。 其中springboot甚至已经不能以火热来形容了, 甚至已经形成了行业某种意义上的”标准“ 。 所以了解并探究springboot的实现是非常有意义的。
  • 本文基于springboot 2.0.4, spring5 的源代码进行分析。

正文

启动第一个springboot应用

  • 下载测试工程
  • 在IDEA上导入项目 并执行SpringBootDemoApplication.class中的main方法
  • 结果如下

image.png

如何启动的

  • 通过代码我们可以看到启动一个springboot应用,非常简单。 只需要一行代码
SpringApplication.run(SpringBootDemoApplication.class, args);
  • debug进去发现其实就是首先new了一个SpringApplication实例,再执行该实例的run方法. 如图

image.png

new SpringApplication发生了什么

  • 先看一下源码
    public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
        this.resourceLoader = resourceLoader;
        Assert.notNull(primarySources, "PrimarySources must not be null");
        this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
        this.webApplicationType = deduceWebApplicationType();
        setInitializers((Collection) getSpringFactoriesInstances(
                ApplicationContextInitializer.class));
        setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
        this.mainApplicationClass = deduceMainApplicationClass();
    }
  • 在new SpringApplication 的时候核心是做了两件事 1. 判断当前spring运行的环境 2. 加载META-INF/spring.factories 并初始化监听器

    • 判断当前spring运行的环境
    • 我们debug到源码里面可以看到, 其实就是根据当前的运行环境有没有某个类来判断的
       private WebApplicationType deduceWebApplicationType() {
           if (ClassUtils.isPresent(REACTIVE_WEB_ENVIRONMENT_CLASS, null)
                   && !ClassUtils.isPresent(MVC_WEB_ENVIRONMENT_CLASS, null)
                   && !ClassUtils.isPresent(JERSEY_WEB_ENVIRONMENT_CLASS, null)) {
               return WebApplicationType.REACTIVE;
           }
           for (String className : WEB_ENVIRONMENT_CLASSES) {
               if (!ClassUtils.isPresent(className, null)) {
                   return WebApplicationType.NONE;
               }
           }
           return WebApplicationType.SERVLET;
       }
    
      
    • 简单来说,目前springboot支持三种运行环境
       public enum WebApplicationType {
    
       /**
        * The application should not run as a web application and should not start an
        * embedded web server.
            * 简单来说就是没有交互的应用程序
        */
       NONE,
    
       /**
        * The application should run as a servlet-based web application and should start an
        * embedded servlet web server.
            * 普通的WEB应用
        */
       SERVLET,
    
       /**
        * The application should run as a reactive web application and should start an
        * embedded reactive web server.
            * 响应式的WEB应用 还比较新 提供了很多新特性 spring4刚刚支持
        */
       REACTIVE
    
      }
    
    • 可以通过加载不同starter来切换环境

image.png
image.png
image.png

  • 加载META-INF/spring.factories

image.png

  • 实际上setInitializers, setListeners这两个方法就是扫描spring.factories类并把实例加载到内存中

执行.run后发生了什么

还是先扫一下源码

    public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection exceptionReporters = new ArrayList<>();
        configureHeadlessProperty();
        SpringApplicationRunListeners listeners = getRunListeners(args);
        listeners.starting();
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(
                    args);
            ConfigurableEnvironment environment = prepareEnvironment(listeners,
                    applicationArguments);
            configureIgnoreBeanInfo(environment);
            Banner printedBanner = printBanner(environment);
            context = createApplicationContext();
            exceptionReporters = getSpringFactoriesInstances(
                    SpringBootExceptionReporter.class,
                    new Class[] { ConfigurableApplicationContext.class }, context);
            prepareContext(context, environment, listeners, applicationArguments,
                    printedBanner);
            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;
    }

其中第一个比较核心是 这两行 获取并启动监听器

    SpringApplicationRunListeners listeners = getRunListeners(args);
   listeners.starting();
  • 首先springboot会首先获取SpringApplicationRunListeners 根据spring.factories的定义 实际上实例化的时候是EventPublishingRunListener这个listener
  • 看一下EventPublishingRunListener的构造函数
    public EventPublishingRunListener(SpringApplication application, String[] args) {
        this.application = application;
        this.args = args;
        this.initialMulticaster = new SimpleApplicationEventMulticaster();
        for (ApplicationListener<?> listener : application.getListeners()) {
            this.initialMulticaster.addApplicationListener(listener);
        }
    }
  • 在这里会把所有通过spring.factories实例化的listener添加到SimpleApplicationEventMulticaster中。
  • 第二步执行starting方法, 会发布一个ApplicationStartingEvent

image.png

  • 我们继续跟进进去,发现了另外一个核心的方法multicastEvent, 我们查一下 有四个listener注册了ApplicationStartingEvent这个事件

image.png

  • 我们随便找了一个LoggingApplicationListener 看一下核心代码
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        //在springboot启动的时候
        if (event instanceof ApplicationStartedEvent) {
            onApplicationStartedEvent((ApplicationStartedEvent) event);
        }
        //springboot的Environment环境准备完成的时候
        else if (event instanceof ApplicationEnvironmentPreparedEvent) {
            onApplicationEnvironmentPreparedEvent(
                    (ApplicationEnvironmentPreparedEvent) event);
        }
        //在springboot容器的环境设置完成以后
        else if (event instanceof ApplicationPreparedEvent) {
            onApplicationPreparedEvent((ApplicationPreparedEvent) event);
        }
        //容器关闭的时候
        else if (event instanceof ContextClosedEvent && ((ContextClosedEvent) event)
                .getApplicationContext().getParent() == null) {
            onContextClosedEvent();
        }
        //容器启动失败的时候
        else if (event instanceof ApplicationFailedEvent) {
            onApplicationFailedEvent();
        }
    }
  • springboot如何判断一个listener支持什么事件。 debug进去可以发现是通过GenericApplicationListener 这个接口实现的

image.png

  • image.png
  • 因为我们的事件类型为ApplicationEvent,所以会执行onApplicationStartedEvent((ApplicationStartedEvent) event);。springBoot会在运行过程中的不同阶段,发送各种事件,来执行对应监听器的对应方法。

小结

  • 根据上文,我们可以简单的做一下小结,在执行了SpringApplication.run(SpringBootDemoApplication.class, args)后发生了什么
  • new了一个SpringApplication实例

    • 判断当前spring运行的环境
    • 加载META-INF/spring.factories 并初始化监听器
  • SpringApplications实例.run

    • 获取并启动监听器
    • 实例化EventPublishingRunListener
    • 把所有通过spring.factories实例化的listener添加到SimpleApplicationEventMulticaster中。
    • 发布ApplicationStartingEvent 执行注册了这个事件的listener
    • 后面还会进行

      • 构造容器环境
      • 创建容器
      • 实例化Failure Analyzers 处理启动的报错信息
      • 准备容器
      • 刷新容器
      • 刷新容器的后扩展接口
    • 后面因为涉及到了spring的核心容器部分,会单独开几章来讲,敬请期待~
目录
相关文章
基于springboot的儿童家长教育能力提升学习系统
本系统聚焦儿童家长教育能力提升,针对家庭教育中理念混乱、时间不足、个性化服务缺失等问题,构建科学、系统、个性化的在线学习平台。融合Spring Boot、Vue等先进技术,整合优质教育资源,提供高效便捷的学习路径,助力家长掌握科学育儿方法,促进儿童全面健康发展,推动家庭和谐与社会进步。
|
人工智能 Java 开发者
【Spring】原理解析:Spring Boot 自动配置
Spring Boot通过“约定优于配置”的设计理念,自动检测项目依赖并根据这些依赖自动装配相应的Bean,从而解放开发者从繁琐的配置工作中解脱出来,专注于业务逻辑实现。
3243 0
|
前端开发 Java 微服务
《深入理解Spring》:Spring、Spring MVC与Spring Boot的深度解析
Spring Framework是Java生态的基石,提供IoC、AOP等核心功能;Spring MVC基于其构建,实现Web层MVC架构;Spring Boot则通过自动配置和内嵌服务器,极大简化了开发与部署。三者层层演进,Spring Boot并非替代,而是对前者的高效封装与增强,适用于微服务与快速开发,而深入理解Spring Framework有助于更好驾驭整体技术栈。
|
XML JSON Java
【SpringBoot(三)】从请求到响应再到视图解析与模板引擎,本文带你领悟SpringBoot请求接收全流程!
Springboot专栏第三章,从请求的接收到视图解析,再到thymeleaf模板引擎的使用! 本文带你领悟SpringBoot请求接收到渲染的使用全流程!
842 3
|
Java 数据库 数据安全/隐私保护
Spring Boot四层架构深度解析
本文详解Spring Boot四层架构(Controller-Service-DAO-Database)的核心思想与实战应用,涵盖职责划分、代码结构、依赖注入、事务管理及常见问题解决方案,助力构建高内聚、低耦合的企业级应用。
2199 1
|
存储 域名解析 弹性计算
阿里云上云流程参考:云服务器+域名+备案+域名解析绑定,全流程图文详解
对于初次通过阿里云完成上云的企业和个人用户来说,很多用户不仅是需要选购云服务器,同时还需要注册域名以及完成备案和域名的解析相关流程,从而实现网站的上线。本文将以上云操作流程为核心,结合阿里云的活动政策与用户系统梳理云服务器选购、域名注册、备案申请及域名绑定四大关键环节,以供用户完成线上业务部署做出参考。
|
前端开发 Java 数据库连接
SpringBoot参数校验底层原理和实操。深度历险、深度解析(图解+秒懂+史上最全)
SpringBoot参数校验底层原理和实操。深度历险、深度解析(图解+秒懂+史上最全)
SpringBoot参数校验底层原理和实操。深度历险、深度解析(图解+秒懂+史上最全)
|
机器学习/深度学习 XML Java
【spring boot logback】日志logback格式解析
在 Spring Boot 中,Logback 是默认的日志框架,它支持灵活的日志格式配置。通过配置 logback.xml 文件,可以定义日志的输出格式、日志级别、日志文件路径等。
2219 5
|
Java 关系型数据库 数据库连接
Spring Boot项目集成MyBatis Plus操作PostgreSQL全解析
集成 Spring Boot、PostgreSQL 和 MyBatis Plus 的步骤与 MyBatis 类似,只不过在 MyBatis Plus 中提供了更多的便利功能,如自动生成 SQL、分页查询、Wrapper 查询等。
1370 2

推荐镜像

更多
  • DNS