Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Spring Kafka, do I need to add the @EnableKafka annotation to my application?

I see some people adding @EnableKafka to their spring boot application and I was wondering why. I have a working spring boot kafka producer and consumer and I didn't use @EnableKafka. So, why do people need to add it explicitly?

Thank you.

like image 707
crillin Avatar asked Apr 16 '20 18:04

crillin


People also ask

What does EnableKafka annotation do?

Annotation Type EnableKafka The KafkaListenerContainerFactory is responsible to create the listener container for a particular endpoint.

How does Kafka integrate with Spring boot application?

React Full Stack Web Development With Spring Boot In this chapter, we are going to see how to implement the Apache Kafka in Spring Boot application. First, we need to add the Spring Kafka dependency in our build configuration file. Maven users can add the following dependency in the pom. xml file.

What does the @KafkaListener annotation do?

Annotation Type KafkaListener. Annotation that marks a method to be the target of a Kafka message listener on the specified topics. The containerFactory() identifies the KafkaListenerContainerFactory to use to build the Kafka listener container.


2 Answers

That is because Spring boot provides an auto configuration for Kafka via KafkaAutoConfiguration class (javadoc). When you use @EnableAutoConfiguration or @SpringBootApplication, Spring boot automatically configures Kafka for you.

You can test that by excluding the auto configuration by providing @SpringBootApplication(exclude={KafkaAutoConfiguration.class}), and Spring boot would not automatically configure Kafka for you.

If you don't use Spring boot, then you would have to use @EnableKafka to configure Kafka for your Spring app.

like image 191
Madhu Bhat Avatar answered Oct 11 '22 13:10

Madhu Bhat


Spring Boot auto-configures @EnableKafka if it detects spring-kafka on the class path.

It is therefore not needed again on a boot app; it is only needed if your Spring app is not a Boot app.

like image 26
Gary Russell Avatar answered Oct 11 '22 13:10

Gary Russell