Spring boot:排除一些自动配置的bean [英] Spring boot: excluding some autoconfigured beans
问题描述
我有一个使用 spring-kafka 的 Spring boot 项目.在这个项目中,我构建了一些包装 spring-kafka bean(即 KafkaTemplate 和 ConcurrentKafkaListenerContainer)的事件驱动组件.我想让这个项目成为一个跨一组 Spring 启动应用程序的可重用库.但是,当我从 Spring Boot 应用程序向该库添加依赖项时,我在应用程序启动时遇到错误:
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.
因为我需要自动装配 ConsumerFactory
(而不是 ConsumerFactory
),所以我在带注释的配置类中创建了这个 bean使用@EnableConfigurationProperties(KafkaProperties.class)
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)
我所需要的只是重用 org.springframework.boot.autoconfigure.kafka.KafkaProperties 而无需在 KafkaAutoConfiguration 和 KafkaAnnotationDrivenConfiguration 中自动配置其他 bean.
All I need is to reuse org.springframework.boot.autoconfigure.kafka.KafkaProperties without other beans being autoconfigured in KafkaAutoConfiguration and KafkaAnnotationDrivenConfiguration.
我尝试将 @EnableAutoConfiguration(exclude = KafkaAutoConfiguration.class) 放在我的库中,但这并不能阻止依赖于我的库的应用程序触发库中排除的 spring-kafka 自动配置.
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.
如何指定我不希望在我的库中自动配置某些 bean(KafkaAutoConfiguration 和 KafkaAnnotationDrivenConfiguration),但也不希望在依赖此库的任何应用程序中自动配置?
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?
推荐答案
我相信依赖于库的应用程序本身必须将这些类从它们的自动配置中排除.
I believe that the apps that depend on the library will themselves have to exclude those classes from their auto configuration.
这篇关于Spring boot:排除一些自动配置的bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!