获取无需 Spring Boot 的依赖
我们推荐在使用 Spring 为 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>1.2.17-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:1.2.17-SNAPSHOT'
}
}
一个最小化的 Apache Pulsar 与 Spring 框架集成所需的依赖项集通常如下所示:
-
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"
}
如果你使用额外功能(如 Reactive),还需要包含相应的依赖项。
Spring for Apache Pulsar 基于 Spring Framework 6.2.17 开发,但应能与任何新的 Spring Framework 6.x 版本正常工作。
许多用户可能会遇到 Spring for Apache Pulsar 的传递依赖解析为 Spring Framework 6.2.17,从而导致奇怪的类路径问题。
解决方法是在您的 dependencyManagement 部分使用 spring-framework-bom,如下所示:
-
Maven
-
Gradle
pom.xml
<dependencyManagement>
<dependencies>
<!-- ... other dependency elements ... -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>6.2.17</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:6.2.17'
}
}
上述示例确保所有 Spring for Apache Pulsar 的传递依赖都使用 Spring 6.2.17 模块。