Search code examples
spring-bootspring-kafka

Spring boot: excluding some autoconfigured beans


I have a Spring boot project that uses spring-kafka. In this project I've built some event driven components that wrap spring-kafka beans (namely KafkaTemplate and ConcurrentKafkaListenerContainer). I want to make this project a reusable library accross a set of Spring boot applications. But when I add dependency to this library from a spring boot app I get an error at app startup:

APPLICATION FAILED TO START


Description:

Parameter 1 of method kafkaListenerContainerFactory in org.springframework.boot.autoconfigure.kafka.KafkaAnnotationDrivenConfiguration required a bean of type 'org.springframework.kafka.core.ConsumerFactory' that could not be found.
    - Bean method 'kafkaConsumerFactory' in 'KafkaAutoConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.kafka.core.ConsumerFactory; SearchStrategy: all) found bean 'kafkaConsumerFactory'


Action:

Consider revisiting the conditions above or defining a bean of type 'org.springframework.kafka.core.ConsumerFactory' in your configuration.

Since I need to autowire a ConsumerFactory<A, B> (and not a ConsumerFactory<Object, Object>) I create this bean in a configuration class that's annotated with @EnableConfigurationProperties(KafkaProperties.class)

All I need is to reuse org.springframework.boot.autoconfigure.kafka.KafkaProperties without other beans being autoconfigured in KafkaAutoConfiguration and KafkaAnnotationDrivenConfiguration.

I've tried to put @EnableAutoConfiguration(exclude = KafkaAutoConfiguration.class) in my library but this is not preventing applications that depends on my library to trigger the spring-kafka autoconfiguration excluded in the library.

How can I specify that I don't want autoconfiguration of some beans (KafkaAutoConfiguration and KafkaAnnotationDrivenConfiguration) in my library but also in any app that depends on this library?


Solution

  • I believe that the apps that depend on the library will themselves have to exclude those classes from their auto configuration.