springboot运行原理详解

2023-11-13


前言

在我们实际使用Spring Boot进行项目开发的过程中,往往只需要几个很简单的注解配置就能够将应用启动运行了,相比于传统的Spring项目而已,这种提升大大地提高了我们的研发效率。

我们往往过多地专注于使用层面,以便快速地完成业务开发,却往往忽略了对框架底层运行原理的关注,接下来全方位地梳理下Spring Boot的底层运行原理,并通过图文结合的方式给大家进行展示,希望对您的工作或者面试能够有所帮助!

开始之前,我们先来看看springboot项目的pom.xml文件吧。

正文

父依赖

其中它主要是依赖一个父项目,主要是管理项目的资源过滤及插件!

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
    </parent>

点击进去,发现还存在一个父依赖:

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.2.6.RELEASE</version>
  </parent>

这个父依赖才是真正管理SpringBoot应用里面所有依赖版本的地方,SpringBoot的版本控制中心。点击进去看看:

 <properties>
    <activemq.version>5.15.12</activemq.version>
    <antlr2.version>2.7.7</antlr2.version>
    <appengine-sdk.version>1.9.79</appengine-sdk.version>
    <artemis.version>2.10.1</artemis.version>
    <aspectj.version>1.9.5</aspectj.version>
    <assertj.version>3.13.2</assertj.version>
    <atomikos.version>4.0.6</atomikos.version>
	...
    <versions-maven-plugin.version>2.7</versions-maven-plugin.version>
    <webjars-hal-browser.version>3325375</webjars-hal-browser.version>
    <webjars-locator-core.version>0.41</webjars-locator-core.version>
    <wsdl4j.version>1.6.3</wsdl4j.version>
    <xml-maven-plugin.version>1.0.2</xml-maven-plugin.version>
    <xmlunit2.version>2.6.4</xmlunit2.version>
  </properties>

我们可以看到里面有许多集成好的依赖了,以后我们导入依赖默认是不需要写版本;但是如果导入的包没有在依赖中管理着就需要手动配置版本了。

主启动类

@SpringBootApplication
public class SpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }

}

接下来分析一下里面包含的各种注解:

@SpringBootApplication

作用:标注在某个类上说明这个类是SpringBoot的主配置类 , SpringBoot就应该运行这个类的main方法来启动SpringBoot应用;

点击进去看看,里面还包含了很多注解:

@Target({ElementType.TYPE}) // 注解的适用范围,其中TYPE用于描述类、接口(包括包注解类型)或enum声明
@Retention(RetentionPolicy.RUNTIME) // 注解的生命周期,保留到class文件中(三个生命周期)
@Documented // 表明这个注解应该被javadoc记录
@Inherited  // 子类可以继承该注解
@SpringBootConfiguration	
@EnableAutoConfiguration
@ComponentScan(						
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
    ...
}

@ComponentScan

这个注解在Spring中很重要 ,它对应XML配置中的元素。

作用:自动扫描并加载符合条件的组件或者bean , 将这个bean定义加载到IOC容器中

@SpringBootConfiguration

作用:SpringBoot的配置类 ,标注在某个类上 , 表示这是一个SpringBoot的配置类;

点击进去这个注解看看:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
    @AliasFor(
        annotation = Configuration.class
    )
    boolean proxyBeanMethods() default true;
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";

    boolean proxyBeanMethods() default true;
}

这里的 @Configuration,说明这是一个配置类 ,配置类就是对应Spring的xml 配置文件;

这里的 @Component 这就说明,启动类本身也是Spring中的一个组件而已,负责启动应用!

回到 SpringBootApplication 注解中继续看。

@EnableAutoConfiguration

@EnableAutoConfiguration

作用:开启自动配置功能,以前我们需要自己配置的东西,而现在SpringBoot可以自动帮我们配置 ;@EnableAutoConfiguration告诉SpringBoot开启自动配置功能,这样自动配置才能生效;

@AutoConfigurationPackage

作用:自动配置包

@Import({Registrar.class})
public @interface AutoConfigurationPackage {
}
@Import

作用:Spring底层注解@import , 给容器中导入一个组件;

Registrar.class 作用:将主启动类的所在包及包下面所有子包里面的所有组件扫描到Spring容器 ;

@Import({AutoConfigurationImportSelector.class})

作用:给容器导入组件

AutoConfigurationImportSelector :自动配置导入选择器,那么它会导入哪些组件的选择器呢?我们点击去这个类看源码:

  1. 这个类有这样一个方法:
// 获得候选的配置
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    //这里的getSpringFactoriesLoaderFactoryClass()方法
    //返回的就是我们最开始看的启动自动导入配置文件的注解类;EnableAutoConfiguration
    List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
    Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
    return configurations;
}
  1. 这个方法又调用了 SpringFactoriesLoader 类的静态方法!我们进入SpringFactoriesLoader类loadFactoryNames() 方法
public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {
    String factoryClassName = factoryClass.getName();
    //这里它又调用了 loadSpringFactories 方法
    return (List)loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
}
  1. loadSpringFactories 方法
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
    //获得classLoader , 我们返回可以看到这里得到的就是EnableAutoConfiguration标注的类本身
    MultiValueMap<String, String> result = (MultiValueMap)cache.get(classLoader);
    if (result != null) {
        return result;
    } else {
        try {
            //去获取一个资源 "META-INF/spring.factories"
            Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");
            LinkedMultiValueMap result = new LinkedMultiValueMap();
 
            //将读取到的资源遍历,封装成为一个Properties
            while(urls.hasMoreElements()) {
                URL url = (URL)urls.nextElement();
                UrlResource resource = new UrlResource(url);
                Properties properties = PropertiesLoaderUtils.loadProperties(resource);
                Iterator var6 = properties.entrySet().iterator();
                
                while(var6.hasNext()) {
                    Entry<?, ?> entry = (Entry)var6.next();
                    String factoryClassName = ((String)entry.getKey()).trim();
                    String[] var9 = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
                    int var10 = var9.length;
                    for(int var11 = 0; var11 < var10; ++var11) {
                        String factoryName = var9[var11];
                        result.add(factoryClassName, factoryName.trim());
                    }
                }
            }
            cache.put(classLoader, result);
            return result;
        } catch (IOException var13) {
            throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var13);
        }
    }
}
  1. spring.factories

在这里插入图片描述

上图就是从SpringBoot的autoconfigure依赖包中的META-INF/spring.factories配置文件中摘录的一段内容,可以很好地说明问题。

@EnableAutoConfiguration自动配置原理:从classpath中搜寻所有的META-INF/spring.factories配置文件,并将其中org.springframework.boot.autoconfigure.EnableutoConfiguration对应的配置项通过反射(Java Refletion)实例化为对应的标注了@Configuration的JavaConfig形式的IoC容器配置类,然后汇总为一个并加载到IoC容器。

对于大部分第三方需要与Spring Boot集成的框架,或者我们日常开发中需要进行抽象的公共组件而言,得益于这种机制,也可以很容易地定制成开箱即用的各种Starter组件。而使用这些组件的用户,往往只需要将依赖引入就好,不再需要进行任何额外的配置了!

以上就是SpringBoot启动的原理了。

借鉴一下网上的springboot启动原理图:

在这里插入图片描述

SpringApplication执行流程

SpringApplication.run分析

分析该方法主要分两部分,一部分是SpringApplication的实例化,二是run方法的执行;

SpringApplication的实例化

调用run之前,首先会实例化一个SpringApplication对象实例;

SpringApplication.run()方法的基本调用流程:

public static ConfigurableApplicationContext run(Class<?> primarySource, String... args{
    return run(new Class[]{primarySource}, args);
}

public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
    return (new SpringApplication(primarySources)).run(args);
}
                                                 
public SpringApplication(Class<?>... primarySources) {
        this((ResourceLoader)null, primarySources);
}

SpringApplication方法:

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
    this.sources = new LinkedHashSet();
    this.bannerMode = Mode.CONSOLE;
    this.logStartupInfo = true;
    this.addCommandLineProperties = true;
    this.addConversionService = true;
    this.headless = true;
    this.registerShutdownHook = true;
    this.additionalProfiles = new HashSet();
    this.isCustomEnvironment = false;
    this.lazyInitialization = false;
    this.resourceLoader = resourceLoader;
    Assert.notNull(primarySources, "PrimarySources must not be null");
    this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
    this.webApplicationType = WebApplicationType.deduceFromClasspath();
    this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
    this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
    this.mainApplicationClass = this.deduceMainApplicationClass();
}

SpringApplication实例初始化完成并且完成设置后,就开始执行run方法:

调用run方法

    public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
        //配置属性
        this.configureHeadlessProperty();
        // 获取监听器
    	// 利用loadFactoryNames方法从路径MEAT-INF/spring.factories中找到所有的SpringApplicationRunListener
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
         // 启动监听
	    // 调用每个SpringApplicationRunListener的starting方法
        listeners.starting();

        Collection exceptionReporters;
        try {
            // 将参数封装到ApplicationArguments对象中
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            // 准备环境
   	        // 触发监听事件——调用每个SpringApplicationRunListener的environmentPrepared方法
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
            // 从环境中取出Banner并打印
            Banner printedBanner = this.printBanner(environment);
            // 依据是否为web环境创建web容器或者普通的IOC容器
            context = this.createApplicationContext();
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
            // 准备上下文
       		// 1.将environment保存到容器中
      		// 2.触发监听事件——调用每个SpringApplicationRunListeners的contextPrepared方法
    		// 3.调用ConfigurableListableBeanFactory的registerSingleton方法向容器中注入applicationArguments与printedBanner
    	    // 4.触发监听事件——调用每个SpringApplicationRunListeners的contextLoaded方法
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            // 刷新容器,完成组件的扫描,创建,加载等
            this.refreshContext(context);
            this.afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
            }
			// 触发监听事件——调用每个SpringApplicationRunListener的started方法
            listeners.started(context);
            this.callRunners(context, applicationArguments);
        } catch (Throwable var10) {
            this.handleRunFailure(context, var10, exceptionReporters, listeners);
            throw new IllegalStateException(var10);
        }

        try {
            listeners.running(context);
            //返回容器
            return context;
        } catch (Throwable var9) {
            this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var9);
        }
    }

总结

SpringApplication.run一共做了两件事

  1. 创建SpringApplication对象;在对象初始化时保存事件监听器,容器初始化类以及判断是否为web应用,保存包含main方法的主配置类。
  2. 调用run方法;准备spring的上下文,完成容器的初始化,创建,加载等。会在不同的时机触发监听器的不同事件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

springboot运行原理详解 的相关文章

随机推荐

  • 深度学习最基础理论知识总结 (CS231课程总结,持续更新)

    因为有在看CS231学习深度学习的简单知识 所以打算整理成blog 持续更新中 一 损失函数loss function 1 SVM 最简单的loss function 其中为真实label对应的分数 为label j对应的分数 Li为每个样
  • C#编程入门基础,开启你的编程之旅

    C 文章有误请指正 如果对你有用 请点赞收藏关注一波 蟹蟹支持 C 简介 C 关键字 C 初次体验 使用 VisualStudio 创建项目 打印 Hello world 1 1 创建项目 1 2 Hello World MSDN 地址 数
  • Openjudge程序设计A_循环结构

    include
  • pytorch训练项目记录时间

    1 首先记录当前时间 import time t0 time time 2 记录结束时间 t1 time time 3 计算时间差 training time t1 t0 4 处理时间格式 import datetime def forma
  • springcloudalibaba项目的搭建

    第一步 搭建父项目 创建一个Maven项目 父项目不写代码 直接删除src 第二步 父项目需要的包 打包方式
  • MSVCP140D.dll没有被指定在Windows上运行,或者它包含错误

    1 xxx dll没有被指定在windows上运行的解决办法 方法一 完全注册系统中的 dll文件 1 开始 运行输入CMD 点击确定或者按下键盘上的回车 Enter 键 打开管理员命令提示符窗口 2 复制 for 1 in windir
  • 此场 X 直火帮 | Set Fire to The Field

    Bzzz说 要有光 就有了光 3D老师给打了光 Bzzz说 要有场 就有了此场atfield 和其他元宇宙分离了出来 Bzzz说 要有趣 就有了元宇宙原生故事和内容 脑洞力才是此场的第一生产力 Bzzz说 要有朋友 就有了坚守创作和初心的梦
  • Python面试题

    Python语言特性 1 Python的函数参数传递 看两个如下例子 分析运行结果 代码一 a 1 def fun a a 2 fun a print a 1 代码二 a def fun a a append 1 fun a print a
  • java蓝桥杯练习 星际交流

    java蓝桥杯练习 星际交流 资源限制 时间限制 1 0s 内存限制 256 0MB 问题描述 人类终于登上了火星的土地并且见到了神秘的火星人 人类和火星人都无法理解对方的语言 但是我们的科学家发明了一种用数字交流的方法 这种交流方法是这样
  • JPA中EntityListeners注解的使用

    使用场景 EntityListeners在jpa中使用 如果你是mybatis是不可以用的 它的意义 对实体属性变化的跟踪 它提供了保存前 保存后 更新前 更新后 删除前 删除后等状态 就像是拦截器一样 你可以在拦截方法里重写你的个性化逻辑
  • docker安装gitlab(离线)

    总体思路 找一台可以联网的linux 下载docker的RPM依赖包而不进行安装 yum localinstall 将所有依赖的rpm环境打包好 再在无网环境中解压逐一安装 rpm force nodeps 同理 docker镜像也通过联网
  • 10 个顶尖的 Linux 开源人工智能工具

    在这篇文章中 我们将介绍几个顶级的开源Linux生态系统的人工智能 AI 工具 目前 AI是科学和技术中不断进步的领域之一 很多人都在致力于构建软件和硬件来解决诸如医疗 教育 安全 制造业 银行等领域的日常挑战 下面是一系列旨在并开发成用于
  • Win32 API和PE-COFF

    一个熟练的Linux程序员可以写一个程序直接和内核交流 比如通过open或者write函数 在Windows则没有那么幸运了 每个新的安装包和Windows NT的发布 都改变了内核的接口 还有对应的库的集合 DLLs给进程提供了一种方式
  • RedisTemplate lettuce 实现分布式锁

    springboot2 x 以上使用redis时 默认使用了lettuce封装 比起jedis线程安全 import lombok extern slf4j Slf4j import org springframework data red
  • 闭关之 Vulkan 应用开发指南笔记(四):绘制、几何体&片段处理、同步和回读数据

    目录 第8章 绘制 8 1 准备绘制 8 2 顶点数据 8 3 索引绘制 8 3 1 只用索引的绘制 8 3 2 重置索引 8 4 实例化 8 5 间接绘制 第9章 几何体处理 9 1 表面细分 9 1 1 表面细分配置 表面细分模式 控制
  • Python笔记(基本入门函数)

    第一章 快速上手 基础知识 1 3 x y x x y y 注意求余运算符 向下圆整 如果是负数 则更远离0 10 3 10 3 10 3 10 3 十六进制 0x 八进制 0o 十进制 0b 0xAF 175 0o10 8 0b10110
  • 微信小程序获取openid的两种方式

    这篇文章是关于获取openid的两种方式 自己在学着用微信小程序来写一个小东西玩 首先我们要到小程序官网获取到自己的appid和Appsecret 如图 第一种 直接在微信小程序中获取 不需要通过后台 登录 wx login success
  • 【数据库】基础知识扫盲

    一 基础知识 关系型数据库 多张表 各表之间的关系 关系 元祖 属性 元组是关系数据库中的基本概念 关系是一张表 表中的一行 即数据库中的每条记录 就是一个元组 表中的一列就是一个属性 关系 表 元祖 表中的一行 属性 表中的一列 码 由一
  • LeetCode 1108. Defanging an IP Address

    import re class Solution def defangIPaddr self address str gt str 这个简单 正则字符串 替换 return re sub address 提交时间 2019 08 03 16
  • springboot运行原理详解

    文章目录 前言 正文 父依赖 主启动类 SpringBootApplication ComponentScan SpringBootConfiguration EnableAutoConfiguration EnableAutoConfig