27

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.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
crillin
  • 273
  • 1
  • 3
  • 5

2 Answers2

39

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.

Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54
18

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.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179