簡體   English   中英

如何將依賴項 class 中的 bean 定義為 Spring 中的 @Primary?

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

我有一個 Kafka Consumer,我正在使用 Spring Cloud Stream Source.class 綁定和 InboundChannelAdapter 來實現它。 這個 Source.class 定義了 3 個 MessageChannel bean:output、nullChannel 和 errorChannel。 我的代碼如下所示:

@EnableBinding(Source.class)
@Import(KafkaSourceConfig.class)
public class KafkaSource {

    @Autowired
    MessageChannel controlBusChannel;

    @InboundChannelAdapter(value = Source.OUTPUT, poller = @Poller(fixedDelay = "1"), autoStartup = "false")
    public AgentActivityNoteCreated consumeAndSendMessage() {
          // UNIMPORTANT CODE
    }
}

我想在 output 通道中自動連接,以便我可以使用它手動啟動和停止我的 InboundChannelAdapter,但在嘗試自動連接時出現此錯誤。

Field controlBusChannel in com.company.KafkaSource required a single bean, but 3 were found:
    - output: defined by method 'output' in null
    - nullChannel: defined in null
    - errorChannel: defined in null
Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

我理解錯誤,應用程序不知道要注入 3 個 bean 中的哪一個,但不知道如何將 output 通道標記為 Primary,因為我實際上並沒有制作 bean。 我怎么做?

如果您的代碼中不存在 Bean 定義。 然后,以下應該工作:

@Autowired
@Qualifier("output")
MessageChannel controlBusChannel;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM