How to set up Kafka Streams topology without defining a Spring Bean?

in several examples of how to set up a topology for Kafka Streams in a SpringBoot based application (e.g. here), a method is defined which is annotated with @Bean. The method has a parameter of type StreamsBuilder (and maybe other parameters) which is the Streams API for defining the topology. Like this:

@Bean
KStream<Integer, String> defineTopology(StreamsBuilder streamBuilder) {
    KStream<integer, String> stream = streamBuilder.stream("streamingTopic1");
    
    // Define the topology

    return stream;
}

Since the method is annotated with @Bean it must return a value (which becomes a Spring Bean). The value is not needed in the application though. All we need from the method is its side effect — achieved via the calls to streamBuilder.

My question is: How can I set up the streams topology without defining a (non needed) Spring Bean?

I’ve thought of using the @PostConstruct annotation but I’m not sure whether the method would then be called at the right place in the application start up procedure.

Leave a Comment