springboot框架学习积累---SpringBoot源码剖析之依赖管理

本文深入剖析SpringBoot中依赖管理机制,重点介绍spring-boot-starter-parent如何统一版本管理,及spring-boot-starter-web如何通过依赖传递引入所需jar包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot框架学习积累—SpringBoot源码剖析之依赖管理

1. 为什么导入dependency时不需要指定版本(spring-boot-starter-parent父依赖启动器进行版本统一管理)

Spring Boot程序中有两个核心的依赖spring-boot-starter-parent,spring-boot-starter-web
  1. spring-boot-starter-parent依赖
    <parent> 
    		<groupId>org.springframework.boot</groupId> 
    		<artifactId>spring-boot-starter-parent</artifactId> 
    		<version>2.2.2.RELEASE</version> 
    		<relativePath/> <!-- lookup parent from repository -->
    </parent>
    
  2. 进入spring-boot-starter-parent并查看,发现底层有一个父依赖spring-boot-dependencies,核心具体代码如下

      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath>../../spring-boot-dependencies</relativePath>
      </parent>
    
    1. 里面的properties标签:定义了编码集的格式,jdk版本,maven编译版本
      	  <properties>
      	    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      	    <java.version>1.8</java.version>
      	    <resource.delimiter>@</resource.delimiter>
      	    <maven.compiler.source>${java.version}</maven.compiler.source>
      	    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      	    <maven.compiler.target>${java.version}</maven.compiler.target>
       	 </properties>
      
    2. 看下面build标签中includes标签,引入外部配置文件,加载顺序是先yml文件,后yaml文件,最后properties文件,所以说properties文件的优先级最高
      <resources>
        <resource>
          <filtering>true</filtering>
          <directory>${basedir}/src/main/resources</directory>
          <includes>
            <include>**/application*.yml</include>
            <include>**/application*.yaml</include>
            <include>**/application*.properties</include>
          </includes>
        </resource>
        ...
      </resources>  
      
      1. 所以说,在SpringBoot的父工程中都做了什么?1. 进行编码的默认设置,JDK的默认版本设置,==2. == 引入一些配置文件,==3. == 插件管理
  3. 继续查看spring-boot-dependencies底层源码,核心代码如下,其中dependencyManagement指定版本,进行版本的锁定,properties只是声明版本

    <properties>
        <activemq.version>5.15.11</activemq.version>
        ...
        <solr.version>8.2.0</solr.version>
        <mysql.version>8.0.18</mysql.version>
        <kafka.version>2.3.1</kafka.version>
        <spring-amqp.version>2.2.2.RELEASE</spring-amqp.version>
        <spring-restdocs.version>2.0.4.RELEASE</spring-restdocs.version>
        <spring-retry.version>1.2.4.RELEASE</spring-retry.version>
        <spring-security.version>5.2.1.RELEASE</spring-security.version>
        <spring-session-bom.version>Corn-RELEASE</spring-session-bom.version>
        <spring-ws.version>3.0.8.RELEASE</spring-ws.version>
        <sqlite-jdbc.version>3.28.0</sqlite-jdbc.version>
        <sun-mail.version>${jakarta-mail.version}</sun-mail.version>
        <tomcat.version>9.0.29</tomcat.version>
        <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
        <thymeleaf-extras-data-attribute.version>2.0.1
        </thymeleaf-extras-dataattribute.version>
                ...
    </properties>
    <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>2.2.9.RELEASE</version>
          </dependency>
          ...
          <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-amqp</artifactId>
            <version>${activemq.version}</version>
      	  </dependency>
     	  ...
      	</dependencies> 
    </dependencyManagement>
    

在这里插入图片描述

2. spring-boot-starter-parent父依赖启动器的主要作用是进行版本统一管理,那么项目运行jar包是从何而来(依赖传递)

  1. 查看spring-boot-starter-webׁ依赖文件源码发现,SpringBoot项目通过依赖spring-boot-starter-webׁ会自动引入SpringMVC的依赖,并且使用tomcat作为内嵌容器,spring-boot-starter-webׁ又依赖了web开发场景所需要的底层的所有依赖(spring-webmvc ...),通过依赖传递的方式来使用项目运行需要的jar
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
          <version>2.2.9.RELEASE</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-json</artifactId>
          <version>2.2.9.RELEASE</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
          <version>2.2.9.RELEASE</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-validation</artifactId>
          <version>2.2.9.RELEASE</version>
          <scope>compile</scope>
          <exclusions>
            <exclusion>
              <artifactId>tomcat-embed-el</artifactId>
              <groupId>org.apache.tomcat.embed</groupId>
            </exclusion>
          </exclusions>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>5.2.8.RELEASE</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.2.8.RELEASE</version>
          <scope>compile</scope>
        </dependency>
      </dependencies>
    
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值