2.2 KiB
1. The on-ramp, and the dependency that is not the one you remember
Under Spring Boot 3 you added org.springframework.kafka:spring-kafka and got a
KafkaTemplate. Under Boot 4 you get this:
No qualifying bean of type 'org.springframework.kafka.core.KafkaTemplate<java.lang.String,
com.ankurm.kafkabasics.OrderEvent>' available
Boot 4 split the auto-configurations out of spring-boot-autoconfigure into per-technology
modules. Kafka's now lives in spring-boot-kafka, package
org.springframework.boot.kafka.autoconfigure, and a bare spring-kafka dependency does not
bring it. The application compiles, the context starts, and there is simply no template.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-kafka</artifactId>
</dependency>
That is the fix, and the same shape applies elsewhere: spring-boot-starter-amqp for RabbitMQ,
spring-boot-starter-restclient for RestClient.Builder. The rule of thumb for Boot 4 is that
if you are depending on a library directly rather than through a Boot starter, you are probably
missing its auto-configuration.
What the starter actually gives you
| Bean | Comes from | Notes |
|---|---|---|
KafkaTemplate<?, ?> |
KafkaAutoConfiguration |
typed by your injection point |
ProducerFactory / ConsumerFactory |
KafkaAutoConfiguration |
built from spring.kafka.* |
KafkaListenerContainerFactory |
KafkaAnnotationDrivenConfiguration |
what @KafkaListener binds to |
KafkaAdmin |
KafkaAutoConfiguration |
creates NewTopic beans at startup |
KafkaAdmin is worth knowing about early: declare a NewTopic bean and Boot creates the topic
on startup with the partition count and replication factor you asked for. It will not change
an existing topic's partition count, so a NewTopic bean that disagrees with the cluster is
silently ignored rather than applied.
Versions
Boot 4.1.1 manages Spring Kafka 4.1.1 and kafka-clients 4.2.1. Note the second one: Maven Central has kafka-clients 4.3.1, and overriding the managed version to reach it is the kind of change that works until it does not. Let the BOM decide.