我有一个使用 spring-kafka 的 Spring Boot 项目。在这个项目中,我构建了一些包装 spring-kafka bean 的事件驱动组件(即 KafkaTemplate 和 ConcurrentKafkaListenerContainer)。我想让这个项目成为跨一组 Spring Boot 应用程序的可重用库。但是,当我从 Spring Boot 应用程序向该库添加依赖项时,在应用程序启动时出现错误:
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.
因为我需要自动装配 a ConsumerFactory<A, B>
(而不是 a ConsumerFactory<Object, Object>
),所以我在一个使用 @EnableConfigurationProperties(KafkaProperties.class) 注释的配置类中创建了这个 bean
我需要的只是重用 org.springframework.boot.autoconfigure.kafka.KafkaProperties,而无需在 KafkaAutoConfiguration 和 KafkaAnnotationDrivenConfiguration 中自动配置其他 bean。
我尝试将@EnableAutoConfiguration(exclude = KafkaAutoConfiguration.class)放入我的库中,但这并不能阻止依赖于我的库的应用程序触发库中排除的 spring-kafka 自动配置。
如何指定我不希望在我的库中以及在依赖此库的任何应用程序中自动配置某些 bean(KafkaAutoConfiguration 和 KafkaAnnotationDrivenConfiguration) ?