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:
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:
application.yml:
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.
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-kafkais 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:
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
KafkaTemplatebean to inject - in Spring Boot, the usual fix is adding
spring-kafkaplus valid producer configuration - if auto-configuration is not enough, define
ProducerFactoryandKafkaTemplatebeans 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

