我们建议在将 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.1.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:1.1.1-SNAPSHOT'
	}
}

A minimal 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"
}

如果使用其他功能(如 Reactive),则还需要包含相应的依赖项。

Spring for Apache Pulsar 针对 Spring Framework 6.1.9 构建,但通常应该适用于任何较新版本的 Spring Framework 6.x。 许多用户可能会遇到这样一个事实,即 Spring for Apache Pulsar 的传递依赖项解决了 Spring Framework 6.1.9,这可能会导致奇怪的类路径问题。 解决此问题的最简单方法是在您的部分中使用,如下所示:spring-framework-bomdependencyManagement

  • Maven

  • Gradle

pom.xml
<dependencyManagement>
	<dependencies>
		<!-- ... other dependency elements ... -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-framework-bom</artifactId>
			<version>6.1.9</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.1.9'
	}
}

前面的示例确保 Spring for Apache Pulsar 的所有传递依赖项都使用 Spring 6.1.9 模块。