首页 / Spring Boot 入门教程 / Starter 依赖体系

Spring Boot 入门教程

Starter 依赖体系

本教程共 48 篇 · 第 8 篇 · 更新于 2026-08-13 · 约 5 分钟阅读

Starter依赖管理MavenBOMspring-boot-starter-parent依赖排除

本节目标:看懂 Starter 的命名规则与常用清单,学会用 parent 和 BOM 管理依赖版本,掌握排除与覆盖依赖。

Starter:一包配齐的依赖

一个 Web 应用要多少依赖?Spring MVC、Tomcat、Jackson、日志……一个个手写版本号,光对齐版本就够呛。Starter 解决这个问题:它是预制的依赖描述符,一个坐标,一包配齐。

Starter 本身几乎不含代码,就是一个依赖清单。官方保证清单里的版本互相兼容。你只管选功能,版本交给 Boot。

命名规则:一眼认出官方货

官方 Starter 统一命名:

spring-boot-starter-<功能名>

比如 spring-boot-starter-webmvcspring-boot-starter-data-jpa。看到 spring-boot-starter- 前缀,就是 Spring 官方出品。

社区 Starter 反过来:<项目名>-spring-boot-starter。比如 MyBatis 的 mybatis-spring-boot-starter。第三方写的 Boot 集成,基本都是这个套路。

Tip

认前缀比记名字快:官方 spring-boot-starter- 开头,社区 xxx-spring-boot-starter 结尾。

常用 Starter 清单

Starter干什么
spring-boot-starter核心:自动配置、日志、YAML
spring-boot-starter-webmvcSpring MVC + Tomcat,写 REST 接口
spring-boot-starter-webflux响应式 Web(WebFlux)
spring-boot-starter-data-jpaSpring Data JPA + Hibernate
spring-boot-starter-data-redisRedis,默认 Lettuce 客户端
spring-boot-starter-jdbcJDBC + HikariCP 连接池
spring-boot-starter-securitySpring Security
spring-boot-starter-validationBean Validation 参数校验
spring-boot-starter-mail邮件发送
spring-boot-starter-test测试全家桶:JUnit、Mockito、Hamcrest
spring-boot-starter-actuator生产监控端点
spring-boot-starter-batchSpring Batch 批处理
spring-boot-starter-cache缓存抽象
Note

4.x 有处改名:spring-boot-starter-web 已弃用,官方推荐 spring-boot-starter-webmvc。旧教程里的 web 坐标还能用,新项目直接写 webmvc

怎么选 Starter

需求先翻译成关键词,再去找坐标。「要写 REST 接口」→ webmvc;「要连 MySQL 做增删改查」→ data-jpajdbc;「要登录鉴权」→ security。拿不准就查官方文档的 starter 列表,或者搜「spring-boot-starter-xxx」。

第三方技术先看官方有没有集成。MyBatis 官方就提供了 mybatis-spring-boot-starter。没有官方集成时,再找社区维护的,认准维护活跃、下载量大的那个。

用法就是加一个依赖,版本都不用写:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webmvc</artifactId>
</dependency>

版本去哪了?被 parent 管着。

版本谁在管:starter-parent

新建项目的 pom 开头,通常是:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>4.1.0</version>
    <relativePath/>
</parent>

spring-boot-starter-parent 干了两件事:继承依赖管理(几百个常用库的版本表)和插件配置(编译、打包插件)。所以子项目加依赖不用写版本。

parent 里的版本和参数都是占位符,能用 <properties> 覆盖。最常用的一个是 Java 版本:

<properties>
    <java.version>21</java.version>
</properties>

编译插件直接读它,一处改,全项目生效。

Note

parent 管理的不只 Spring 自己的库,还有 Jackson、Hibernate 这些第三方库。升级 Boot 版本,整套依赖跟着升级,兼容性由官方把关。

不用 parent:直接导入 BOM

公司项目可能已经有一个 parent。没关系,BOM 可以单独导入。BOM(Bill of Materials)是纯版本清单,spring-boot-dependencies 就是 Boot 的 BOM:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>4.1.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

导入后效果一样:Starter 不用写版本。区别是插件配置没了,spring-boot-maven-plugin 要自己加。Gradle 项目更常用 BOM 方案:

dependencies {
    implementation platform("org.springframework.boot:spring-boot-dependencies:4.1.0")
    implementation "org.springframework.boot:spring-boot-starter-webmvc"
}

覆盖版本:想改也能改

管理的版本是「默认值」,不是「锁死」。想覆盖某个依赖的版本,显式写 version 即可。Maven 里,当前 pom 直接声明的版本优先于父级管理。

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.19.1</version>
</dependency>
Warning

覆盖版本前先想清楚:Boot 管理的版本是经过兼容性测试的。强行升级某个库,可能引入不兼容。生产环境改版本,务必跑一遍测试。

排除依赖:拿掉不想要的

Starter 是「全家桶」,有时里面有你不需要的东西。用 <exclusions> 拿掉。最常见场景:换嵌入式容器。

4.x 里,Tomcat 运行组件由 spring-boot-starter-tomcat-runtime 提供。换 Jetty 就这么写:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webmvc</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat-runtime</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty-runtime</artifactId>
</dependency>
Note

版本差异:2.x/3.x 时代,容器组件是 spring-boot-starter-tomcatspring-boot-starter-jetty,排除的也是这两个坐标。4.x 改为 -runtime 后缀的 starter。另外 4.0 移除了 Undertow——它没适配 Servlet 6.1,嵌入式容器只剩 Tomcat 和 Jetty 两家。

排除依赖另一个用途:spring-boot-starter-webmvc 自带的 Jackson 满足不了需求时,排除掉换别的 JSON 库。操作一样,改坐标即可。

Tip

排查「奇怪依赖」用 mvn dependency:tree。它打印完整依赖树,谁带来了谁一目了然。排除之后也用它确认拿掉了。

Note

Gradle 的排除语法不同:implementation("org.springframework.boot:spring-boot-starter-webmvc") { exclude group: "org.springframework.boot", module: "spring-boot-starter-tomcat-runtime" }。思路一样,写法换一下。

Starter 和自动配置是一对

Starter 管「依赖到位」,自动配置管「配好」。你加 spring-boot-starter-data-jpa,Hibernate 等 jar 进 classpath;jar 里的 .imports 文件被 Boot 读到,自动配置类评估条件、创建 Bean。第 6 章的两块拼图,在这里拼上了。

「加 Starter 就自动能用」不是巧合:依赖是触发器,自动配置是执行者。

小结

  • Starter 是预制依赖清单,一个坐标配齐一套功能。
  • 官方命名 spring-boot-starter-*,社区 *-spring-boot-starter
  • parent 或 BOM 管版本,显式 version 可覆盖。
  • <exclusions> 排除依赖;4.x 换容器用 -runtime 系列 starter。