Ziyan Li
Ziyan Li

Reputation: 43

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.

Upvotes: 3

Views: 1944

Answers (2)

grayswander
grayswander

Reputation: 93

This is just an IDE false alert. You can suppress this error in IDE by adding

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") 

Upvotes: 0

Artem Bilan
Artem Bilan

Reputation: 121177

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.

Upvotes: 2

Related Questions