说起Spring Boot项目中的依赖管理,Ma ven几乎是绕不开的标配。Spring Boot专门提供了一个starter POM——spring-boot-starter-parent,它就像是依赖管理的“预制菜”,让项目一继承就能坐享其成。下面就来拆解一下,怎么在Spring Boot项目中用好Ma ven做依赖管理。

前言
日常写Ja va,Ma ven和Spring Boot基本是形影不离的。不少新手容易困惑:dependencies和dependencyManagement到底差在哪?多模块项目怎么管依赖才不乱?为什么一个Starter就能拉出一堆功能?spring-boot-starter-parent、spring-boot-starter、spring-boot-starter-web这几个家伙各管哪一摊?单元测试还得多加个spring-boot-starter-test?Spring Boot各个版本又该配什么JDK?
本文就来系统捋一捋这些知识点,直接上干货。
一、Ma ven 依赖管理
1.1 dependencies
作用
它的作用很直接:真正把依赖“拉”进来。
举个例子,父工程这样写:
pom org.projectlombok lombok 1.18.42
子模块里就算不写任何dependency,也会自动继承lombok。实际上连上面那一段子模块中的dependency都可以省略,Ma ven会自动传递给所有子模块。
想验证的话,跑一句:
mvn dependency:tree
整棵依赖树一览无余。
1.2 dependencyManagement(推荐)
作用
专门用来统一管版本,但不自动引入依赖。写法如下:
pom org.apache.shardingsphere shardingsphere-jdbc 5.5.2
子模块使用时,只需声明坐标,不用写版本:
org.apache.shardingsphere shardingsphere-jdbc
Ma ven会从父工程的dependencyManagement里自动找到版本号。
这里有个关键点:子模块必须自己写dependency,否则依赖不会出现。比如父工程只管理了MySQL驱动的版本,子模块如果不写对应的dependency,项目里根本用不了MySQL。
1.3 两者区别
| 项目 | dependencies | dependencyManagement |
|---|---|---|
| 是否引入依赖 | ✅ | ❌ |
| 是否管理版本 | ✅ | ✅ |
| 子模块自动拥有 | ✅ | ❌ |
| 企业项目推荐 | 公共依赖 | 统一版本 |
二、多模块项目最佳实践
2.1 推荐结构
下面是一套常见的多模块布局:
root-parent │ ├── user-service ├── order-service ├── pay-service └── common
父工程打包类型必须是pom:
pom
2.2 统一管理版本
父工程中通过dependencyManagement统一管控所有第三方库的版本:
mysql mysql-connector-j 9.3.0 org.apache.shardingsphere shardingsphere-jdbc 5.5.2
2.3 公共依赖
每个模块都需要的公共库(比如lombok、日志门面等),放到父工程的dependencies里,所有子模块自动继承。
org.projectlombok lombok
2.4 企业项目推荐模式
整体套路可以总结成一句话:父工程用dependencyManagement锁定版本,用dependencies放公共依赖;子模块按需写dependencies,版本由父工程统一配。
三、Spring Boot 版本与 JDK 对照表
选版本之前先看这张对照表,心里有底:
| Spring Boot | Spring Framework | 最低 JDK |
|---|---|---|
| 1.x | 4.x | Ja va 7 |
| 2.0~2.7 | 5.x | Ja va 8 |
| 3.x | 6.x | Ja va 17 |
| 4.x | 7.x | Ja va 17 |
JDK8 推荐版本
Spring Boot 2.7.18
这是Spring Boot 2.x的最后一个正式版,稳得很。
JDK17 推荐版本
Spring Boot 3.x
常见组合
老项目
JDK8 Spring Boot 2.7.18 MyBatis MySQL Redis
新项目
JDK17 Spring Boot 3.x Spring Cloud 2025+
四、Spring Boot Starter 机制
4.1 什么是 Starter
Starter本质上就是一个依赖聚合包,干的事很简单:一次性把一组兼容的依赖全部拉进来。
比如引用spring-boot-starter-web:
org.springframework.boot spring-boot-starter-web
它背后实际引入了:
spring-web spring-webmvc tomcat jackson validation spring-boot spring-boot-autoconfigure
4.2 Starter 的真正价值
Starter ≠ 自动配置。它的价值必须和AutoConfiguration配合才能体现:
Starter + AutoConfiguration = 开箱即用
举个例子,配置Redis:
spring:
redis:
host: localhost
然后在代码里直接注入:
@Autowired private RedisTemplate redisTemplate;
就能用。背后实际上是:
Redis Starter
↓
RedisAutoConfiguration
↓
自动创建 Bean
五、spring-boot-starter-parent
5.1 是什么
它就是Ma ven里的一个父工程:
org.springframework.boot spring-boot-starter-parent 2.7.18
5.2 作用
统一管理版本
引入任何Spring Boot starter都不用写版本号,比如:
org.springframework.boot spring-boot-starter-web
版本由parent统一搞定。
管理插件版本
像ma ven-compiler-plugin、ma ven-surefire-plugin、spring-boot-ma ven-plugin这些常用插件的版本也一并管好。
提供默认配置
比如编码UTF-8、Ja va版本、资源过滤规则等,都不用自己配。
5.3 注意
它只负责Ma ven层面的配置管理,并不提供Spring Boot功能。真正的功能是靠starter和自动配置来实现的。
六、spring-boot-starter
6.1 是什么
Spring Boot的核心启动器,引入它:
org.springframework.boot spring-boot-starter
6.2 引入内容
spring-core spring-context spring-beans spring-aop spring-boot spring-boot-autoconfigure logging
6.3 不包含
没有 spring-web、spring-webmvc、tomcat,所以直接用@RestController会报错。
七、spring-boot-starter-web
注意:正确名称是
spring-boot-starter-web,不是spring-boot-web。
7.1 是什么
专门用于Web开发的启动器:
org.springframework.boot spring-boot-starter-web
7.2 依赖结构
spring-boot-starter-web │ ├── spring-boot-starter ├── spring-web ├── spring-webmvc ├── jackson ├── validation └── tomcat
注意:spring-boot-starter-web已经包含了spring-boot-starter,所以Web项目只需要引入这一个就够。
7.3 可以直接使用
引入后就能直接写@RestController、@RequestMapping、@GetMapping、@PostMapping等Web注解。
八、spring-boot-starter-test
8.1 为什么需要
Web Starter不带测试框架。要搞单元测试,得额外引入:
org.springframework.boot spring-boot-starter-test test
8.2 包含内容
JUnit5 Mockito AssertJ Hamcrest Spring Test JSONassert
8.3 常见测试
SpringBootTest
@SpringBootTest
class UserServiceTest {
@Autowired
private UserService userService;
@Test
void test() {
}
}
Mockito
@ExtendWith(MockitoExtension.class)
class UserServiceTest {
@Mock
private UserMapper userMapper;
@InjectMocks
private UserService userService;
}
8.4 scope=test
的作用范围:
| 阶段 | 可用 |
|---|---|
| main编译 | ❌ |
| test编译 | ✅ |
| test运行 | ✅ |
| 打包Jar | ❌ |
这样测试框架就不会混进生产环境的包里。
九、必须使用 starter-parent 吗?
不是必须,Spring Boot官方支持两种方式。
方式1(推荐)
org.springframework.boot spring-boot-starter-parent 2.7.18
然后直接引入starter,版本不用写——最简单。
方式2(企业项目常见)
很多公司有自己的父工程:
com.company company-parent
Ma ven只能有一个parent,这时候就不能继承spring-boot-starter-parent了。替代方案是用spring-boot-dependencies做BOM导入:
org.springframework.boot spring-boot-dependencies 2.7.18 pom import
效果和继承starter-parent几乎一样。
十、三者关系图
spring-boot-starter-parent │ ├── 管理版本 ├── 管理插件 └── 不提供功能 spring-boot-starter │ ├── spring-core ├── spring-context ├── spring-boot └── logging spring-boot-starter-web │ ├── spring-boot-starter ├── spring-web ├── spring-webmvc ├── jackson └── tomcat
九、Spring Boot 项目标准依赖模板
以JDK8 + Spring Boot 2.7.18为例,一个典型的pom.xml长这样:
org.springframework.boot spring-boot-starter-parent 2.7.18 org.springframework.boot spring-boot-starter-web org.projectlombok lombok true org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-test test
十、总结
Ma ven
dependencies
↓
真正引入依赖
dependencyManagement
↓
统一管理版本
Spring Boot
spring-boot-starter-parent
↓
管理版本和插件
spring-boot-starter
↓
Boot核心功能
spring-boot-starter-web
↓
Web开发
spring-boot-starter-test
↓
单元测试
推荐组合
JDK8
Spring Boot 2.7.18
JDK17+
Spring Boot 3.x
多模块项目
父工程 ├── dependencyManagement(统一版本) ├── dependencies(公共依赖) └── modules 子模块 └── dependencies(按需引入)
