不使用 Spring Boot 获取依赖项
我们建议在使用 Spring for Apache Pulsar 时采用 Spring Boot 优先的方法。然而,如果您不使用 Spring Boot,获取依赖项的首选方法是使用提供的 BOM,以确保在整个项目中使用一致的模块版本。以下示例展示了如何在 Maven 和 Gradle 中做到这一点:
- Maven
- Gradle
<dependencyManagement>
<dependencies>
<!-- ... other dependency elements ... -->
<dependency>
<groupId>org.springframework.pulsar</groupId>
<artifactId>spring-pulsar-bom</artifactId>
<version>1.2.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
plugins {
id "io.spring.dependency-management" version "1.1.4"
}
dependencyManagement {
imports {
mavenBom 'org.springframework.pulsar:spring-pulsar-bom:1.2.3'
}
}
一个最小的 Apache Pulsar 的 Spring 依赖集通常如下所示:
- Maven
- Gradle
<dependencies>
<!-- ... other dependency elements ... -->
<dependency>
<groupId>org.springframework.pulsar</groupId>
<artifactId>spring-pulsar</artifactId>
</dependency>
</dependencies>
dependencies {
implementation "org.springframework.pulsar:spring-pulsar"
}
如果您使用额外的功能(例如 Reactive),您还需要包含适当的依赖项。
Spring for Apache Pulsar 依赖于 Spring Framework 6.2.3,但通常应该可以与任何更新版本的 Spring Framework 6.x 一起使用。许多用户可能会遇到 Spring for Apache Pulsar 的传递依赖解析为 Spring Framework 6.2.3 的问题,这可能会导致奇怪的类路径问题。解决此问题的最简单方法是在您的 dependencyManagement
部分中使用 spring-framework-bom
,如下所示:
- Maven
- Gradle
<dependencyManagement>
<dependencies>
<!-- ... other dependency elements ... -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>6.2.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
plugins {
id "io.spring.dependency-management" version "1.1.4"
}
dependencyManagement {
imports {
mavenBom 'org.springframework:spring-framework-bom:6.2.3'
}
}
前面的例子确保了 Spring 对 Apache Pulsar 的所有传递依赖都使用 Spring 6.2.3 模块。