Spring boot: excluding some autoconfigured beans

馋奶兔 提交于 2019-12-24 02:28:27

问题


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?


回答1:


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




回答2:


putting the same in servlet initializer worked for me.

 @SpringBootApplication(exclude = KafkaAutoConfiguration.class)
public class Application extends SpringBootServletInitializer{

        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application .class);
        }


        public static void main(String[] args) {
            SpringApplication.run(GncApplication.class, args);
        }

    }


来源:https://stackoverflow.com/questions/43633263/spring-boot-excluding-some-autoconfigured-beans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!