SpringBoot特点1-依赖管理特性-03

SpringBoot 特点

依赖管理

  • 每一个 SpringBoot 工程,都有一个父项目,spring-boot-starter-parent 依赖作为 Spring Boot 项目的统一父项目依赖管理;
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
    </parent>
  • 按住 ctrl,点击下图红色部分,就可以进入底层源文件,发现 spring-boot-starter-parent 底层有一个的父依赖 spring-boot-dependencies;
 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.3.5.RELEASE</version>
  </parent>
  • spring-boot-dependencies 几乎声明了所有开发中常用的依赖的版本号,自动版本仲裁机制。进入spring-boot-dependencies底层源文件,其核心代码如下:
  <properties>
    <activemq.version>5.15.13</activemq.version>
    <antlr2.version>2.7.7</antlr2.version>
    <appengine-sdk.version>1.9.82</appengine-sdk.version>
    <artemis.version>2.12.0</artemis.version>
    <aspectj.version>1.9.6</aspectj.version>
    <assertj.version>3.16.1</assertj.version>
    <atomikos.version>4.0.6</atomikos.version>
    <awaitility.version>4.0.3</awaitility.version>
    <bitronix.version>2.1.4</bitronix.version>
    <build-helper-maven-plugin.version>3.1.0</build-helper-maven-plugin.version>
    <byte-buddy.version>1.10.17</byte-buddy.version>
    <caffeine.version>2.8.6</caffeine.version>
    <cassandra-driver.version>4.6.1</cassandra-driver.version>
    <classmate.version>1.5.1</classmate.version>
    <commons-codec.version>1.14</commons-codec.version>
    <commons-dbcp2.version>2.7.0</commons-dbcp2.version>
    ......
    <tomcat.version>9.0.39</tomcat.version>
    <unboundid-ldapsdk.version>4.0.14</unboundid-ldapsdk.version>
    <undertow.version>2.1.4.Final</undertow.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.45</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.7.0</xmlunit2.version>
  </properties>
  • 从底层 spring-boot-dependencies 底层源文件可以看出,该文件通过标签对一些常用技术框架的依赖文件进行了统一版本号管理,依赖管理里面,父项目把我们常用开发中的 jar 包版本都帮我们弄好了,并且这些版本都是我们当前 SpringBoot 支持的版本。这也就是pom.xml 引入依赖文件不需要标注依赖(dependency) jar 版本号的原因。
  • 总结:spring-boot-starter-parent 父依赖启动器的主要作用是进行版本统一管理;父项目中可能会生成非常多的依赖,子项目只要继承了这个父项目,子项目以后写依赖就不需要版本号了。

引入非版本仲裁的 jar

  • 查看 spring-boot-dependencies 里面规定当前依赖的版本。
  • 搜索Maven仓库,找到满意的驱动。https://mvnrepository.com/
  • 如果我们想要的版本号与自动仲裁的版本号不一样,我们可以修改驱动。在 pom.xml 文件中,重写 properties:
    <properties>
        <mysql.version>5.1.43</mysql.version>
    </properties>

依赖启动器

  • 我们开发一个 web 应用,无需关心到底要导入哪些 jar 包,只需要在 pom.xml 文件里导入 spring-boot-starter-web;
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
  • 那么项目运行依赖的 jar 包是从何而来?进入 spring-boot-starter-web 底层源文件,核心代码如下:
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.3.5.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-json</artifactId>
      <version>2.3.5.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.3.5.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.10.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.10.RELEASE</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
  • 也可以通过右键 spring-boot-starter-web -> Diagrams -> Show Dependencies,查看分析依赖树;
    在这里插入图片描述
    在这里插入图片描述
  • 可以看出,spring-boot-starter-web 依赖启动器主要作用是提供了 web 开发场景需要的底层所有依赖。所以在 pom.xml 中引入 spring-boot-starter-web 依赖启动器时,就可以实现 web 场景开发,而不需要额外导入 Tomcat 服务器依赖以及其他 web 依赖文件。
  • 但是,这些引入依赖的文件版本还是由 spring-boot-starter-parent 进行统一管理。

开发导入 starter 场景依赖启动器

  • Spring Boot 除了提供上述 web 依赖启动器以外,还提供了许多场景所需要的依赖 spring-boot-starter-* : * 代表各种某种场景。
  • 只要引入 starter,这个场景的所有常规需要的依赖我们都自动引入。
  • SpringBoot 所有支持的场景依赖启动器:https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter
  • 见到的 *-spring-boot-starter: 这是第三方为我们提供的简化开发的场景依赖启动器,例如:mybatis-spring-boot-starter 等。在需要的时候直接在 pom.xml 文件中导入即可,但是需要自己管理版本号。
  • 所有场景启动器最底层的依赖都是 spring-boot-starter。
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.3.5.RELEASE</version>
      <scope>compile</scope>
    </dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玥玥&编程学习记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值