如果您使用的是 Spring Boot,那么这三个依赖项将由以下启动器为您提供:
-
spring-test 将由 spring-boot-starter-test 提供
-
spring-context 将由 spring-boot-starter-data-jpa 提供
-
spring-jdbc 将由 spring-boot-starter-jdbc 提供
因此,使用以下父级:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
...如果添加这些依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
...然后你会得到
但是,Spring Boot 1.5.6.RELEASE 依赖于这些核心 Spring 库的 v4.3.10.RELEASE not 4.3.9.RELEASE 如您的问题中所建议的那样。通常,您会接受 Spring 的依赖管理,因此如果 Sping 提供 4.3.10.RELEASE,那么 (a) 您应该使用该版本或 (b) 将 Spring Boot 降级为提供 4.3.9.RELEASE 的版本。
继续阅读以了解如何为给定的精选库识别正确的启动器...
spring-boot-starter-parent 是一个特殊的启动器,它提供有用的 Maven 默认值和一个依赖管理部分,它定义了您可能希望在 POM 中使用的大量依赖项。这些依赖通常被称为“curated”或“blessed”,因为它们是在 maven 层次结构中某处的依赖管理部分中定义的,您可以在没有版本标签的 POM 中引用它们(即它们从依赖管理部分条目。)
您可以看到spring-boot-starter-parent POM here 并查看内部您可以看到它引用了spring-boot-dependencies POM here。
查看您提到的问题,您可以像这样声明依赖项......
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
...这是因为spring-boot-dependencies POM 声明了以下内容:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${revision}</version>
</dependency>
因此,parent 和 starters 只是封装依赖声明并让应用程序开发人员更容易使用它们的一种方式。 Spring docs 总结为:
Starters 是一组方便的依赖描述符,您可以将它们包含在您的应用程序中。您可以获得所需的所有 Spring 和相关技术的一站式商店,而无需搜索示例代码和复制粘贴加载的依赖描述符。例如,如果您想开始使用 Spring 和 JPA 进行数据库访问,请在项目中包含 spring-boot-starter-data-jpa 依赖项。
然而,这 not 意味着所有依赖项都必须通过 parent 或 starters 声明,因此,如果您 不 使用 Spring Boot,那么您可以在不使用的情况下声明依赖项父母或初学者以及您在问题中描述的内容(声明对 3 个核心 Spring 库的依赖项)可以通过明确地依赖这 3 个库来安全地涵盖。例如,只需将以下内容添加到您的pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.9.RELEASE</version>
<scope>test</scope>
</dependency>