Spring Cloud 流无法自动装配 Source.class [英] Spring cloud streams could not autowire Source.class

查看:87
本文介绍了Spring Cloud 流无法自动装配 Source.class的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从头开始学习 Spring Cloud Streams.

I am learning Spring Cloud Streams from scratch.

我尝试创建这样的 Source 应用程序:

I tried to create a Source application like this:

import org.springframework.cloud.stream.messaging.Source; //etc
@RestController
@SpringBootApplication
@CrossOrigin
@EnableBinding(Source.class)
public class StreamsProducerApplication {

    @Autowired
    Source source;

    @GetMapping(value="/send/{message}")
    public void sendMessage(@PathVariable String message){
        if(message != null){

     source.output().send(MessageBuilder.withPayload(message).build());}
}

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

}

但是,我从 Intellij IDEA 的Source source"得到错误提示;说无法自动装配.未找到‘源’类型的 bean.

However, I get error hint from Intellij IDEA at "Source source;" saying "Could not autowire. No beans of 'Source' type found.

我可以理解Source是我从中导入的接口,但是spring官网说Spring Cloud Stream为您创建了该接口的实现.您可以通过自动装配在应用程序中使用它"https://docs.spring.io/spring-cloud-stream/docs/current/reference/htmlsingle/

I can understand that Source is a interface from where I import, but the spring official website says "Spring Cloud Stream creates an implementation of the interface for you. You can use this in the application by autowiring it" https://docs.spring.io/spring-cloud-stream/docs/current/reference/htmlsingle/

那我是怎么做错的呢?谢谢.

So how did I do this wrong? Thank you.

推荐答案

只是 Intellij IDEA 不知道 @EnableBinding(Source.class) 将在运行时成为一个 bean.只有这样的 bean 定义,所以工具无法确定在 @Autowired 中注入什么.

It is just the Intellij IDEA doesn't know that @EnableBinding(Source.class) is going to be a bean at runtime. There is just on such a bean definition, so tooling fails to determine what to inject in that @Autowired.

否则,您的代码完全没有问题,您只需要运行它并使用您对该代码的期望即可.

Otherwise your code is fully good and you just need to run it and play with whatever you expect from that code.

这篇关于Spring Cloud 流无法自动装配 Source.class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆