Skip to content
Advertisement

How to define a bean from a dependency class as @Primary in Spring?

I have a Kafka Consumer and I’m implementing it using the Spring Cloud Stream Source.class binding and InboundChannelAdapter. This Source.class defines 3 MessageChannel beans: output, nullChannel, and errorChannel. My code looks like this:

JavaScript

I want to autowire in the output channel so that I can use it to start and stop my InboundChannelAdapter manually, but I’m getting this error when trying to autowire.

JavaScript

I understand the error, the app doesnt know which of the 3 beans to inject, but don’t know how to mark the output channel as Primary since I didn’t actually make the bean. How do I do that?

Advertisement

Answer

If the Bean definition is not present in your code. Then, following should work:

JavaScript

Reference: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-autowired-annotation-qualifiers

Advertisement