Spring Framework
KafkaTemplate
Programming
Java
Error Solving

required a bean of type 'org.springframework.kafka.core.KafkaTemplate' that could not be found

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

This Spring error means the application context tried to inject a KafkaTemplate, but no matching bean was registered. The fix is usually one of three things: add the correct Spring Kafka dependency, provide enough producer configuration for auto-configuration to create the bean, or define the KafkaTemplate manually.

What Spring Is Looking For

A typical producer service looks like this:

java
1import org.springframework.kafka.core.KafkaTemplate;
2import org.springframework.stereotype.Service;
3
4@Service
5public class EventPublisher {
6    private final KafkaTemplate<String, String> kafkaTemplate;
7
8    public EventPublisher(KafkaTemplate<String, String> kafkaTemplate) {
9        this.kafkaTemplate = kafkaTemplate;
10    }
11
12    public void publish(String topic, String payload) {
13        kafkaTemplate.send(topic, payload);
14    }
15}

If the context cannot construct a KafkaTemplate<String, String>, startup fails with the message you saw.

Let Spring Boot Auto-Configure It

In a normal Spring Boot application, the easiest path is to include Spring Kafka and provide producer settings.

build.gradle:

gradle
dependencies {
    implementation 'org.springframework.kafka:spring-kafka'
}

application.yml:

yaml
1spring:
2  kafka:
3    bootstrap-servers: localhost:9092
4    producer:
5      key-serializer: org.apache.kafka.common.serialization.StringSerializer
6      value-serializer: org.apache.kafka.common.serialization.StringSerializer

With the dependency present and producer properties defined, Spring Boot can usually create the producer factory and KafkaTemplate automatically.

Define the Bean Manually When Needed

If you want explicit control or auto-configuration is not active, define the producer factory and template yourself.

java
1import java.util.HashMap;
2import java.util.Map;
3import org.apache.kafka.clients.producer.ProducerConfig;
4import org.apache.kafka.common.serialization.StringSerializer;
5import org.springframework.context.annotation.Bean;
6import org.springframework.context.annotation.Configuration;
7import org.springframework.kafka.core.DefaultKafkaProducerFactory;
8import org.springframework.kafka.core.KafkaTemplate;
9import org.springframework.kafka.core.ProducerFactory;
10
11@Configuration
12public class KafkaProducerConfig {
13
14    @Bean
15    public ProducerFactory<String, String> producerFactory() {
16        Map<String, Object> props = new HashMap<>();
17        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
18        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
19        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
20        return new DefaultKafkaProducerFactory<>(props);
21    }
22
23    @Bean
24    public KafkaTemplate<String, String> kafkaTemplate() {
25        return new KafkaTemplate<>(producerFactory());
26    }
27}

Once this configuration class is scanned, the bean becomes available for injection.

Common Reasons the Bean Is Missing

The most common causes are straightforward:

  • 'spring-kafka is not on the classpath'
  • producer configuration is missing or malformed
  • the manual config class is outside component scanning
  • the application is using a test slice or reduced context that excludes Kafka config
  • the injected generic type does not match the bean you created

That last one matters when you use typed templates such as KafkaTemplate<String, MyEvent>.

Check Tests Separately

This error often appears in tests because a slim Spring test configuration does not load your Kafka producer beans.

For example, a web-layer slice test may not include Kafka configuration unless you import it explicitly or mock the dependency.

In those cases, the pragmatic fix is often:

java
1import org.springframework.boot.test.mock.mockito.MockBean;
2import org.springframework.kafka.core.KafkaTemplate;
3
4@MockBean
5private KafkaTemplate<String, String> kafkaTemplate;

That lets the test focus on the unit under test instead of requiring a full Kafka producer setup.

Common Pitfalls

Adding Kafka properties without the spring-kafka dependency is a common mistake. Configuration alone does not create the library classes.

Defining a manual KafkaTemplate bean in a package that Spring does not scan is another frequent cause.

Assuming all tests load the full application context also leads to confusion. Many test annotations intentionally create smaller contexts.

Finally, if you use custom serializers and typed payloads, make sure the producer factory and template generics line up with what you inject.

Summary

  • the error means Spring could not find a KafkaTemplate bean to inject
  • in Spring Boot, the usual fix is adding spring-kafka plus valid producer configuration
  • if auto-configuration is not enough, define ProducerFactory and KafkaTemplate beans manually
  • check component scanning and test-slice behavior if the bean appears only missing in some contexts
  • keep template generics and serializer configuration consistent with the payload type you are sending

Course illustration
Course illustration

All Rights Reserved.