本文使用Spring Boot 版本:org.springframework.boot:spring-boot-autoconfigure:2.3.1RELEASE
起步依赖
起步依赖本质上是一个Maven项目对象模型(Project Object Model,POM),定义了对其它库的传递依赖,通过这些依赖的组合支持某项功能。这个在起步依赖的命名上是有所体现的。
在Spring Boot项目的源码中我们可以看到spring-boot-starters的子项目
打开其中的任意一个子项目,只有一个build.gradle文件:
plugins {
id "org.springframework.boot.starter"
}
description = "Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container"
dependencies {
api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-json"))
api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-tomcat"))
api("org.springframework:spring-web")
api("org.springframework:spring-webmvc")
}
在这里能看到起步依赖相关的传递依赖。
1. 指定基于功能的依赖
起步依赖的版本是由正在使用的Spring Boot的版本来决定的,所以我们不需要指定起步依赖的版本,而起步依赖会决定它们引入的传递依赖的版本。
可以在构建工具中找到所用依赖的版本,在IDEA工具中能看到类似下图的依赖树:
2. 覆盖起步依赖引入的传递依赖
可以通过构建工具中的功能,排除传递依赖,也可以为起步依赖没有涵盖的库指定依赖
在Gradle中排除传递依赖,build.gradle文件:
implementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
覆盖起步依赖引入的传递依赖的版本号,这里需要注意的是Grad了更倾向于使用最新的版本,如果指明的依赖版本高于起步依赖引入的传递依赖的版本号,则不需要排除起步依赖引入的传递依赖,反之,则需要排除起步依赖引入的传递依赖
在Gradle中覆盖起步依赖,build.gradle文件:
implementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation('org.junit.vintage:junit-vintag