在 Spring Kafka 中,我是否需要将 @EnableKafka 注释添加到我的应用程序中?

cri*_*lin 13 java apache-kafka spring-boot spring-kafka

我看到有些人将 @EnableKafka 添加到他们的 Spring Boot 应用程序中,我想知道为什么。我有一个正在工作的 Spring Boot kafka 生产者和消费者,但我没有使用 @EnableKafka。那么,为什么人们需要明确添加它呢?

谢谢你。

Mad*_*hat 20

这是因为 Spring Boot 通过KafkaAutoConfiguration类 ( javadoc )为 Kafka 提供了自动配置。当您使用@EnableAutoConfiguration或 时@SpringBootApplication,Spring Boot 会自动为您配置 Kafka。

您可以通过提供排除自动配置来测试 @SpringBootApplication(exclude={KafkaAutoConfiguration.class}),并且 Spring boot 不会自动为您配置 Kafka。

如果您不使用 Spring boot,则必须使用@EnableKafkaKafka 为您的 Spring 应用程序配置。


Gar*_*ell 10

@EnableKafka如果Spring Boot在类路径上检测到 spring-kafka,它会自动配置。

因此,启动应用程序不再需要它;仅当您的 Spring 应用程序不是 Boot 应用程序时才需要它。