我正在尝试使用 Spring Cloud Stream 来发布和使用 Kafka 消息。我一直在研究文档这里关于访问绑定通道 http://docs.spring.io/spring-cloud-stream/docs/Chelsea.SR2/reference/htmlsingle/index.html#_accessing_bound_channels。我试图在我的主题的频道上使用自定义名称,因此当我尝试注入它时我有一个@Qualifier,但 spring 找不到相关的 bean。它说“对于每个绑定接口,Spring Cloud Stream 将生成一个实现该接口的 bean”,但自动装配不起作用。
我收到的错误是“com...MessagingManager 中的构造函数的参数 0 需要一个类型为 'org.springframework.messaging.MessageChannel' 的 bean,但无法找到。”
我尝试在 MessagingManager 构造函数之前使用 @Autowired (如示例中所示),但随后在 bean 工厂中出现了类似的错误,其中有 2 个,所以我将其取出,并得到了当前错误。
我尝试使用处理器可能会让事情变得复杂。
这是我的组件。我正在使用 Spring Boot 运行它并尝试使用以下命令对其进行测试:
@Component
public class StartupTester implements ApplicationListener<ContextRefreshedEvent> {
MessagingManager messagingManager;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
messagingManager.sendThingCreatedMessage(new ThingCreated("12345", "667788"));
}
}
@Component
public class MessagingManager {
private MessageChannel thingCreatedChannel;
public MessagingManager(@Qualifier(ThingChannelProcessor.THING_CREATED) MessageChannel output) {
thingCreatedChannel = output;
}
public void sendThingCreatedMessage(ThingCreated thingCreated) {
thingCreatedChannel.send(MessageBuilder.withPayload(thingCreated).build());
}
}
@Component
public interface ThingsChannelProcessor extends Processor {
String THING_REQUEST = "thing-request";
String THING_CREATED = "thing-created";
@Input(THING_REQUEST )
SubscribableChannel thingsRequest();
@Output(THING_CREATED )
MessageChannel thingCreated();
}
我的主类上也有 @EnableBinding(ThingsMessagingManager.class),它用 @SpringBootApplication 注释。
我无法重现您的错误。但我有几点你可以遵循:
- 您不需要使用以下方式注释接口
@Component
- 您的内容似乎有错字
@EnableBinding
你应该有@EnableBinding(ThingsChannelProcessor.class)
不是 ThingsMessagingManager
- 您也不需要扩展 Processor,这可能是您第一次获得 2 个 bean 的原因。如果您正在自定义通道,则不需要从 Sink/Source/Processor 下降,请查看
Barista
文档中的示例
- 监听 contextRefresh 也不起作用,因为我们在刷新上下文后进行绑定
事实上,让我更清楚一点4
。我们创建了一个子上下文,因此为了确保您的上下文已完全初始化,请确保您还在 Starter 上实现了 ApplicationContextAware,并在发送消息之前检查上下文是否相同,否则您将收到错误if(this.context.equals(event.getApplicationContext()))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)