背景

前几天新建了一个空的Maven项目,当引入SpringBoot依赖的时候蒙了,之前新建项目时,都是无脑copy之前的pom文件,官方语言是保证各个项目的版本一致,内心os能copy干嘛要动脑。很少想过这几个start文件具体有什么区别,导致应该引入哪些文件。趁着这个机会,稍微整理一下。

正文

start文件介绍

start文件也被称为起步依赖。根据包根路径不同,大概为了两类:官方start依赖和三方依赖。其中所有官方starters遵循相似的命名模式:spring-boot-starter-应用名,第三方start为了避免跟Spring Boot官方artifacts冲突,其名称通常命名为应用名-spring-boot-starter

Spring Boot 将日常企业应用研发中的各种场景都抽取出来,做成一个个的 starter(启动器),starter 中整合了该场景下各种可能用到的依赖,用户只需要在 Maven 中引入 starter 依赖,Spring Boot 就能自动扫描到要加载的信息并启动相应的默认配置。starter 提供了大量的自动配置,让用户摆脱了处理各种依赖和配置的困扰。所有这些 starter 都遵循着约定成俗的默认配置,并允许用户调整这些配置,即遵循“约定大于配置”的原则。

SpringBoot项目应用最多的基础start文件有以下几类:

依赖名称

作用

spring-boot-starter-parent

Spring Boot的版本仲裁中心,控制了所有依赖的版本号

spring-boot-starter-web

自动引入了web模块开发需要的相关jar包

spring-boot-starter-test

springboot程序测试依赖

spring-boot-dependencies

自动配置实现包

spring-boot-starter

Spring Boot的核心启动器,包含了自动配置、日志和YAML

// Spring Boot的版本仲裁中心,控制了所有依赖的版本号,
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath/>
</parent>

// Spring Boot的核心启动器,包含了自动配置、日志和YAML
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

// web的场景,自动引入了web模块开发需要的相关jar包
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

// springboot程序测试依赖,如果是自动创建项目默认添加
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

// 自动配置实现包,所有的自动配置类都可以在此包中找到包含多个optional和test范围的依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.

 

 

start文件详情

这么多start文件,都是必须要的吗?它们之间有什么关系呢?之前无脑copy的时候,还真没有想过这个问题?

spring-boot-starter-parent

先看spring-boot-starter-parent,点进去可以看到其实真正起作用的是spring-boot-dependencies,此外spring-boot-starter-parent说明了配置文件的格式。

SpringBoot项目创建时引入的starter依赖_YAML

对于spring-boot-dependencies作用则比较清楚了,它更像一个版本仓库,声明了基础组件依赖的版本,之后引用的时候不用再声明版本了。所以如果在项目中引用了spring-boot-starter-parent,就不用再引用spring-boot-dependencies了。若同时有spring-boot-starter-parent和spring-boot-dependencies,依旧以spring-boot-parent中的版本为准。

<properties>
    <activemq.version>5.15.7</activemq.version>
    <antlr2.version>2.7.7</antlr2.version>
    <appengine-sdk.version>1.9.67</appengine-sdk.version>
    <artemis.version>2.6.3</artemis.version>
    <aspectj.version>1.9.2</aspectj.version>
    <assertj.version>3.11.1</assertj.version>
    <atomikos.version>4.0.6</atomikos.version>
    <bitronix.version>2.1.4</bitronix.version>

    ....
</properties>
<dependencyManagement>
  <dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot</artifactId>
          <version>2.1.0.RELEASE</version>
      </dependency>
      ....
  <dependencies>
</dependencyManagement>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
spring-boot-starter-web

然后看一下spring-boot-starter-web,这个依赖也是用的特别多。点进去可以看到里面包含的依赖。



spring-boot-starter

Spring Boot的核心启动器,基本每个start依赖都存在它。

spring-boot-starter-json

使用jackson处理请求参数类型匹配和解析

spring-boot-starter-tomcat

内嵌tomcat,从而处理客户端发送的 HTTP 请求

hibernate-validator

主要是用于验证对象的属性是否符合特定的规则和约束

spring-web

springMVC中的注解

spring-webmvc

spring-boot-starter

spring-boot-starter,Spring Boot的核心启动器,包含了自动配置、日志和YAML。基本每个start依赖都存在它,如spring-boot-starter-test。简单看了一下spring-boot-starter里面的内容,包含了spring-core,spring-boot-autoconfigure,spring-boot-starter-logging。再细节的内容可能还需要再细节的去研究一下。

总结

一个提供http接口的SpringBoot项目不能缺少的start依赖有两个:spring-boot-starter-parent,spring-boot-starter-web。一个用来声明版本,一个用到嵌入tomcat。当然这句话不可细究。对于spring-boot-dependencies,spring-boot-starter则可以省略。

后续需要整理一下SpringBoot的整体架构,才能从整体看理解和掌握start的机制吧。