问题
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