Spring cloud streams could not autowire Source.class

痴心易碎 提交于 2020-02-02 15:42:50

问题


I am learning Spring Cloud Streams from scratch.

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);
}

}

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

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.


回答1:


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.



来源:https://stackoverflow.com/questions/53497582/spring-cloud-streams-could-not-autowire-source-class

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