Spring boot: excluding some autoconfigured beans

I have a Spring boot project that uses spring-kafka. In this project, I have created some event related beans that complete the spring-kafka beans (namely KafkaTemplate and ConcurrentKafkaListenerContainer). I want to make this project a reusable library through a set of Spring Boot Applications. But when I add a dependency on this library from a Spring Boot application, I get an error when starting the application:

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 ConsumerFactory<A, B>

(not ConsumerFactory<Object, Object>

), I create this bean in the config class which is annotated with @EnableConfigurationProperties (KafkaProperties.class)

All I need is to reuse org.springframework.boot.autoconfigure.kafka.KafkaProperties, without using other beans auto-configured in KafkaAutoConfiguration and KafkaAnnotationDrivenConfiguration.

I tried to put @EnableAutoConfiguration (exclude = KafkaAutoConfiguration.class) in my library, but that doesn't prevent applications that depend on my library from starting the spring-kafka autoconfiguration excluded in the libraries.

How can I indicate that I do not need auto-configuration of some beans (KafkaAutoConfiguration and KafkaAnnotationDrivenConfiguration) in my library, but also in any application that depends on this library ?

+1


source to share


1 answer


I believe that library-dependent applications will themselves have to exclude these classes from their automatic configuration.



+2


source







All Articles