获取无需 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>2.0.5-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.5-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"
}
如果您使用了额外的功能,还需要包括相应的依赖项。
Spring for Apache Pulsar 基于 Spring Framework 7.0.6 开发,但通常也适用于任何 newer 的 Spring Framework 6.x 版本。
许多用户可能会遇到 transitive 依赖解析为 Spring Framework 7.0.6 的问题,这会导致奇怪的 classpath 问题。
解决方法是在你的 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.6</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.6'
}
}
上述示例确保了所有 Spring for Apache Pulsar 的 transitive 依赖都使用 Spring 7.0.6 模块。