liveBook · Manning

Spring bean

As explained in Kafka Streams in Action, Second Edition

A Spring bean is an object managed and configured by the Spring application container.

A Spring bean is an object managed and configured by the Spring application container. Bean definitions supply infrastructure that application components use, such as a Kafka listener-container factory or Kafka Streams configuration. With Spring Boot, the container can create useful defaults; explicit beans replace or extend them when an application needs different behavior. section 12.2.2 section 12.3

Bean naming determines how a component finds a configured object. A @Bean method normally uses its method name as the bean name. Naming a custom ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory shadows the Boot default, so existing @KafkaListener methods use the custom factory without code changes. Alternatively, give the bean another name and select it explicitly with @KafkaListener(containerFactory = "..."). section 12.2.2

Listing 12.11 Increasing the concurrency level for a container listener factory
@Bean
public ConcurrentKafkaListenerContainerFactory<String, LoanApplication>
                    kafkaListenerContainerFactory() {        #1
    ConcurrentKafkaListenerContainerFactory<String, LoanApplication>
           kafkaListenerContainerFactory =
            new ConcurrentKafkaListenerContainerFactory<>();
    kafkaListenerContainerFactory.setConsumerFactory(consumerFactory());
    kafkaListenerContainerFactory.setConcurrency(partitions);    #2
    return kafkaListenerContainerFactory;
}

A listener-container-factory bean sets concurrency to the input topic's partition count. The resulting factory creates a consumer per partition for maximum throughput. section 12.2.2

Beans also provide Kafka Streams lifecycle and customization hooks:

  • Define the configuration bean with the required defaultKafkaStreamsConfig name so Spring Boot can create a StreamBuilderFactoryBean and StreamsBuilder. section 12.3
  • Define a KafkaStreamsCustomizer bean to access KafkaStreams before startup, for example to attach a StateListener that logs active task assignments. section 12.3
  • Define a StreamsBuilderFactoryBeanCustomizer bean that applies the KafkaStreamsCustomizer to the factory bean. section 12.3

Configuring a loan-processing listener

A loan-application service can expose a custom listener-container-factory bean, attach its consumer factory, and set concurrency from the configured partition count. Keeping the default bean name lets the existing loan listener pick up the higher-throughput configuration automatically; a separately named factory requires an explicit containerFactory attribute on the listener. section 12.2.2

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains bean naming, default overriding, explicit selection, and Kafka Streams customizer roles without general container internals.
Examples
Uses loan-application listeners, partition-based concurrency, default Kafka Streams configuration, and a running-state task-assignment logger.
What you can do after
Apply it via Spring Boot to override listener factories or configure and customize Kafka Streams startup behavior.
How it connects
Connects beans to Spring Boot, @KafkaListener, Kafka Streams, KafkaStreams, StreamsBuilderFactoryBean, and StateListener.

Also explained in

Compare how 3 books explain Spring bean →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.