本文介绍了没有可用的“org.springframework.kafka.config.AbstractKafkaListenerContainerFactory"类型的合格 bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想禁用自动启动的 KAFKA 消费者并尝试以下代码

I want to disable KAFKA consumer being autostarted and tried the below code

@Component
class Customizer {

    Customizer(AbstractKafkaListenerContainerFactory<?, ?, ?> factory,
            @Value("${start.containers:true}") boolean start) {

        factory.setAutoStartup(start);
    }

}

当我禁用测试用例时它工作正常,但是在执行测试用例时我看到以下错误.

it works fine when I disable the Test cases however I see the below error while test cases are getting executed.

java.lang.IllegalStateException: 加载 ApplicationContext 失败引起的:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为 'kafkaConsumerCustomizer' 的 bean 时出错文件 [kafkaConsumerCustomizer.class]:表示不满足的依赖通过构造函数参数0;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:否符合类型的 bean'org.springframework.kafka.config.AbstractKafkaListenerContainerFactory'可用:预计至少有 1 个符合自动装配条件的 bean候选人.

我添加了空的默认构造函数以使测试用例通过,但是它总是引用空的默认构造函数而不是参数化的构造函数.有没有办法只对测试用例使用空的默认构造函数,否则使用参数化构造函数?

I have added the empty default constructor to make the test cases pass however it is always referring the empty default constructor but not the parameterized constructor. Is there a way to use the empty default constructor only for the test cases and parameterized constructor otherwise?

我尝试了以下解决方案

解决方案#1:

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.kafka.config.AbstractKafkaListenerContainerFactory;
    import org.springframework.stereotype.Component;
    
     
    
    @Component
    class KafkaConsumerCustomizer {
      KafkaConsumerCustomizer() {
      }
    
     
    
      KafkaConsumerCustomizer(AbstractKafkaListenerContainerFactory<?, ?, ?> factory,
             @Value("${consumer.autostart:true}") boolean start) {
        factory.setAutoStartup(start);
      }
    }

对于解决方案#1:默认构造函数被执行,无法启动/停止消费者

For Solution#1:the default constructor got executed and could not start/stop the consumers

解决方案#2:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.config.AbstractKafkaListenerContainerFactory;
import org.springframework.stereotype.Component;

 

@Component
class KafkaConsumerCustomizer {
  @Autowired
  AbstractKafkaListenerContainerFactory<?, ?, ?> factory;
  
  @Value("${consumer.autostart:true}")
  private boolean start;

 

  KafkaConsumerCustomizer() {
    factory.setAutoStartup(start);
    System.out.println("Setting Autostart: " + start);
  }
}

对于解决方案#2:不满足的依赖通过构造函数参数 0 表示;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的org.springframework.kafka.config.AbstractKafkaListenerContainerFactory"类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选.依赖注释:{}u2028 at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:767)

For Solution#2:Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.kafka.config.AbstractKafkaListenerContainerFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}u2028 at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:767)

如何在不破坏测试用例的情况下禁用自动启动 KAFKA 消费者?

How to disable KAFKA consumer being autostarted without breaking the test cases ?

更新:已从 KafkaConsumerCustomizer 类中删除了 @Component 标签

Update:Have removed the @Component tag from KafkaConsumerCustomizer class

class KafkaConsumerCustomizer {
  KafkaConsumerCustomizer(AbstractKafkaListenerContainerFactory<?, ?, ?> factory,
         @Value("${consumer.autostart:true}") boolean start) {
    factory.setAutoStartup(start);
  }
}

以及我更新了主配置类

@Getter
@RefreshScope
@Import(processorConfig.class)
@Configuration
public class appSpringConfiguration {
  @Bean
  KafkaConsumerCustomizer kafkaCustomizer() {
    return new KafkaConsumerCustomizer();
  }
}

它仍然抛出以下错误:

SpringConfiguration.java:[49,12] 构造函数KafkaConsumerCustomizer在类 com.xx.KafkaConsumerCustomizer 中不能应用于给定类型;[错误] 需要:org.springframework.kafka.config.AbstractKafkaListenerContainerFactory,boolean[错误] 发现:没有参数 [错误] 原因:实际和正式参数列表的长度不同 [错误] ->[帮助1]

推荐答案

想必,你的测试没有标记 @SpringBootTest.

Presumably, your tests are not marked @SpringBootTest.

您可以在测试配置中添加一个 mock(AbstractKafkaListenerContainerFactory) bean,或者不要在定制器上使用 @Component ;而是在主配置中为它添加一个 @Bean.

You can either add a mock(AbstractKafkaListenerContainerFactory) bean to your test configuration, or don't use @Component on the customizer; instead add a @Bean for it in the main configuration.

然后,它不会在您的测试中通过组件扫描找到.

Then, it won't be found by component scanning in your tests.

在其他一些@Configuration 类中,添加

In some other @Configuration class, add

@Bean
KafkaConsumerCustomizer cust() {
    return new KafkaConsumerCustomizer();
}

或...

@Bean
KafkaConsumerCustomizer cust(AbstractKafkaListenerContainerFactory<?, ?, ?> factory,
            @Value("${start.containers:true}") boolean start) {
    return new KafkaConsumerCustomizer(factory, start);
}

这篇关于没有可用的“org.springframework.kafka.config.AbstractKafkaListenerContainerFactory"类型的合格 bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 04:26