2017-05-25 23 views
1

我一直在尋找Spring集成ip模塊,我想創建UDP通道接收,但我發現我只能用XML來完成。 我一直在想,如果我查看實現代碼的內部,我可以做出一些事情,但它會根據xml中提供的參數創建bean本身。 我不能在我的代碼中使用xml定義,有沒有辦法讓它在沒有xml的情況下使用spring?春天整合ip - udp通道只有java代碼

或者,有什麼更好的方法在java中使用udp?

+0

事實證明,正如答案中指出的那樣,我在查看UdpInboundChannelAdapterParser而不是org.springframework.integration.ip.udp.UnicastReceivingChannelAdapter。 – Sarief

回答

1

5.0版本開始有關於此事的Java DSL了,所以對UDP通道適配器的代碼可能看起來像:

@Bean 
    public IntegrationFlow inUdpAdapter() { 
     return IntegrationFlows.from(Udp.inboundAdapter(0)) 
       .channel(udpIn()) 
       .get(); 
    } 

    @Bean 
    public QueueChannel udpIn() { 
     return new QueueChannel(); 
    } 

    @Bean 
    public IntegrationFlow outUdpAdapter() { 
     return f -> f.handle(Udp.outboundAdapter(m -> m.getHeaders().get("udp_dest"))); 
    } 

但與現有的Spring集成版本,你可以簡單地配置UnicastReceivingChannelAdapter豆:

@Bean 
public UnicastReceivingChannelAdapter udpInboundAdapter() { 
     UnicastReceivingChannelAdapter unicastReceivingChannelAdapter = new UnicastReceivingChannelAdapter(1111); 
     unicastReceivingChannelAdapter.setOutputChannel(udpChannel()); 
     return unicastReceivingChannelAdapter; 
} 

在參考手冊中,您可以找到Tips and Tricks一章,瞭解如何使用原始Java和註釋配置編寫Spring Integration應用程序。

我添加了JIRA來解決參考手冊中的Java示例。

+0

哇,像魅力一樣工作,謝謝:)你是怎麼找到它/找出你必須使用它? ... upd:看着配置文件,現在有些道理:) – Sarief