在没有 Spring Boot 的情况下获取依赖项
我们推荐在使用 Spring for Apache Pulsar 时采用 Spring Boot 优先的方法。但是,如果您不使用 Spring Boot,获取依赖项的首选方式是使用提供的 BOM,以确保整个项目中使用的模块版本一致。以下示例展示了如何在 Maven 和 Gradle 中实现这一点
-
Maven
-
Gradle
pom.xml
<dependencyManagement>
<dependencies>
<!-- ... other dependency elements ... -->
<dependency>
<groupId>org.springframework.pulsar</groupId>
<artifactId>spring-pulsar-bom</artifactId>
<version>2.0.1-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
build.gradle
plugins {
id "io.spring.dependency-management" version "1.1.4"
}
dependencyManagement {
imports {
mavenBom 'org.springframework.pulsar:spring-pulsar-bom:2.0.1-SNAPSHOT'
}
}
最少的 Spring for Apache Pulsar 依赖项集通常如下所示
-
Maven
-
Gradle
pom.xml
<dependencies>
<!-- ... other dependency elements ... -->
<dependency>
<groupId>org.springframework.pulsar</groupId>
<artifactId>spring-pulsar</artifactId>
</dependency>
</dependencies>
build.gradle
dependencies {
implementation "org.springframework.pulsar:spring-pulsar"
}
如果您使用附加功能,您还需要包含相应的依赖项。
Spring for Apache Pulsar 基于 Spring Framework 7.0.1 构建,但通常应与任何更新的 Spring Framework 6.x 版本兼容。许多用户可能会遇到 Spring for Apache Pulsar 的传递依赖项解析为 Spring Framework 7.0.1 的问题,这可能导致奇怪的类路径问题。解决此问题的最简单方法是在您的 dependencyManagement 部分中使用 spring-framework-bom,如下所示
-
Maven
-
Gradle
pom.xml
<dependencyManagement>
<dependencies>
<!-- ... other dependency elements ... -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>7.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
build.gradle
plugins {
id "io.spring.dependency-management" version "1.1.4"
}
dependencyManagement {
imports {
mavenBom 'org.springframework:spring-framework-bom:7.0.1'
}
}
前面的示例确保 Spring for Apache Pulsar 的所有传递依赖项都使用 Spring 7.0.1 模块。