Spring-Kafka How to pass the kafka topic from the application.yml
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Passing a Kafka topic name from application.yml is a standard Spring Boot pattern because topic names often change by environment. Instead of hard-coding "orders" or "payments" in Java code, you define the topic once in configuration and inject it wherever producers or listeners need it.
Defining the Topic in application.yml
The cleanest approach is to put application-specific values under your own prefix instead of forcing everything into Spring Kafka's built-in properties. That keeps transport settings separate from business topic names.
Now the topic is externalized and can be overridden for local, staging, or production environments.
Using @Value for a Simple Case
If you only need one or two values, @Value is enough:
This is easy to read and works well for small applications. The topic can now be changed without touching Java code.
Using @ConfigurationProperties for Multiple Topics
Once your project has several topics, @ConfigurationProperties scales better. It is type-safe, easier to test, and avoids repeating property keys all over the codebase.
Register the properties class:
Then inject it:
Passing the Topic into a Listener
Spring Kafka also lets you resolve property placeholders in annotations such as @KafkaListener. That means the listener topic can live in YAML too.
This is often the most convenient option for consumers because the mapping between configuration and listener is direct.
Why This Is Better Than Hard-Coding
Externalized topics make deployment safer. A developer can point the same code at a test topic locally and a production topic in a real cluster by changing configuration only. It also makes renaming topics less invasive because the value lives in one place rather than across multiple source files.
This approach also supports profile-specific files such as application-dev.yml and application-prod.yml, which is useful when clusters and topic names differ across environments.
Common Pitfalls
One common mistake is storing business topic names under unrelated built-in keys just because Spring Kafka already has a spring.kafka section. That can make the configuration harder to understand. Use your own prefix for application-level topic names.
Another issue is mismatched property names. For example, order-topic in YAML maps to orderTopic in a configuration properties class. If the naming does not line up, the injected value may stay null.
A third pitfall is hard-coding the producer topic but externalizing the listener topic. That creates drift over time. If a topic is meant to be configurable, keep it configurable everywhere.
Finally, remember that putting a topic name in YAML does not create the topic in Kafka. Topic existence, partitions, and retention settings are still operational concerns unless your platform auto-creates them.
Summary
- Define Kafka topic names in
application.ymlso they can vary by environment. - Use
@Valuefor one or two simple properties. - Use
@ConfigurationPropertieswhen you have several topics or want type-safe configuration. - '
@KafkaListener(topics = "${...}")can read topic names directly from YAML.' - Externalized topic names improve maintainability and reduce hard-coded environment details.
Related reading
- Spring-Kafka vs. kafka-clients directly
- Spring-Kafka vs. kafka-clients directly
- Spring / RabbitMQ transaction management
- Spring Actuator + Kafka Streams - Add kafka stream status to health check endpoint
- spring4.2.1, hibernate5 integrate abstract method error
- Spring - Multiple Spring Data modules found, entering strict repository configuration mode
- Spring AMQP - Sender and Receiving Messages
- Spring AMQP + RabbitMQ 3.3.5 ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.