2017-07-27 74 views
1

我正在嘗試使用Spring Cloud Stream來發布和使用Kafka消息。我一直在關注文檔here on Accessing Bound Channels。我試圖在我的主題上使用頻道的自定義名稱,所以當我嘗試注入時有一個@Qualifier,但是Spring找不到相關的bean。它說「對於每個綁定的接口,Spring Cloud Stream將生成一個實現接口的bean」,但是自動佈線不起作用。爲什麼不能春天找到spring雲流創建的@Source bean頻道?

我得到的錯誤是「com中構造函數的參數0 ... MessagingManager需要一個'org.springframework.messaging.MessageChannel'類型的bean無法找到。」

我試着在MessagingManager構造函數之前使用@Autowired,就像在例子中那樣,但是後來在bean工廠裏得到了一個類似的錯誤,因爲它們中有2個,所以我把它拿出來,並且得到了當前的錯誤。

這可能是複雜的,我試圖使用一個處理器。

這是我的組件。我和春天開機運行,並試圖與這個來測試它:

@Component 
public class StartupTester implements ApplicationListener<ContextRefreshedEvent> { 
    MessagingManager messagingManager; 

    @Override 
    public void onApplicationEvent(ContextRefreshedEvent event) { 
     messagingManager.sendThingCreatedMessage(new ThingCreated("12345", "667788")); 
    } 
} 

@Component 
public class MessagingManager { 

    private MessageChannel thingCreatedChannel; 

    public MessagingManager(@Qualifier(ThingChannelProcessor.THING_CREATED) MessageChannel output) { 
     thingCreatedChannel = output; 
    } 

    public void sendThingCreatedMessage(ThingCreated thingCreated) { 
thingCreatedChannel.send(MessageBuilder.withPayload(thingCreated).build()); 
    } 
} 


@Component 
    public interface ThingsChannelProcessor extends Processor { 

    String THING_REQUEST = "thing-request"; 
    String THING_CREATED = "thing-created"; 

    @Input(THING_REQUEST) 
    SubscribableChannel thingsRequest(); 

    @Output(THING_CREATED) 
    MessageChannel thingCreated(); 
} 

而且我也有@EnableBinding(ThingsMessagingManager.class)在其上標註有@SpringBootApplication我的主類。

+0

嘗試將接口作爲參數傳遞@EnableBinding(ThingsChannelProcessor.class) – RSM

回答

1

我無法重現您的錯誤。但是,我有幾個點,你可以遵循:

  1. 你不需要註釋界面@Component
  2. 看來你有一個錯字你@EnableBinding你應該有@EnableBinding(ThingsChannelProcessor.class)沒有ThingsMessagingManager
  3. 你也不需要延長處理器,這可能是你第一次獲得2個豆的原因。如果您正在自定義您的頻道,則無需從接收器/來源/處理器下降,查看文檔中的Barista示例
  4. 聽取contextRefresh也不起作用,因爲我們在上下文之後執行綁定被刷新了

實際上,讓我對4更清楚些。我們創建一個子上下文,所以爲了確保你的上下文已經完全初始化,確保你在你的Starter上實現了ApplicationContextAware,並且在發送消息之前檢查上下文是否相同,否則你會得到一個錯誤if(this.context.equals(event.getApplicationContext()))