没有Spring Boot如何获得依赖
我们建议在使用 Apache Pulsar 时先使用 Spring Boot 方法。 不过,如果你不使用 Spring Boot,获取依赖的首选方式是使用提供的物料清单,以确保整个项目中使用的模块版本一致。 以下示例展示了如何为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.12</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.12'
}
}
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"
}
如果你使用额外功能(如响应式),还需要包含相应的依赖关系。
Apache Pulsar 的 Spring 构建基于 Spring Framework 6.2.13,但通常应能兼容任何更新版本的 Spring Framework 6.x。
许多用户可能会遇到 Spring for Apache Pulsar 的传递依赖解决 Spring Framework 6.2.13 的问题,这可能导致奇怪的类路径问题。
解决这个问题最简单的方法是使用Spring-框架-BOM在你的依赖管理部分如下:
-
Maven
-
Gradle
pom.xml
<dependencyManagement>
<dependencies>
<!-- ... other dependency elements ... -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>6.2.13</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.13'
}
}
前述示例确保 Spring for Apache Pulsar 的所有传递依赖都使用 Spring 6.2.13 模块。