在没有 Spring Boot 的情况下获取依赖

我们建议在使用 Spring for 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.6-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.6-SNAPSHOT'
	}
}

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.2.6 构建,但通常应与任何较新的 Spring Framework 6.x 版本一起使用。许多用户可能会遇到 Spring for Apache Pulsar 的传递依赖项解析 Spring Framework 6.2.6 的情况,这可能会导致奇怪的类路径问题。解决此问题的最简单方法是在您的 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.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:6.2.6'
	}
}

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