Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring cloud streams could not autowire Source.class

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.

like image 532
Ziyan Li Avatar asked Feb 18 '26 21:02

Ziyan Li


1 Answers

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.

like image 142
Artem Bilan Avatar answered Feb 20 '26 15:02

Artem Bilan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!