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


