gpt4 book ai didi

java - Spring Boot找不到EmbeddedKafkaBroker Bean

转载 作者:行者123 更新时间:2023-12-02 09:34:51 25 4
gpt4 key购买 nike

我正在为 Kafka 编写集成测试。为此,我使用 Spring 的 EmbeddedKafka。这是我的类(class):

@Component
@EmbeddedKafka(
partitions = 1,
topics = {"Topic},
brokerProperties = {"listeners=PLAINTEXT://localhost:9091", "port=9091"})
public class KafkaConsumerTestUtils {
@Autowired private EmbeddedKafkaBroker embeddedKafka;
....
}

我在用@SpringBootTest注释的类中 Autowiring 这个utils类。这是我收到的错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.kafka.test.EmbeddedKafkaBroker' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1646)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1205)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1166)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
... 66 more

奇怪的是,当我在任何测试类中 Autowiring EmbeddedKafkaBroker 时,它就会被创建并且测试运行良好。

有人可以帮我解决这个问题吗?

最佳答案

不太清楚为什么,但看起来您没有按照预期的方式使用它。

@EmbeddedKafka 是一个注释,可以在运行基于 Spring Kafka 的测试的测试类上指定。

文档说明:

The typical usage of this annotation is like:
@RunWith(SpringRunner.class)
@EmbeddedKafka
public class MyKafkaTests {}

关于java - Spring Boot找不到EmbeddedKafkaBroker Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57620561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com