Spring 集成:摆脱设置 bean 的代码重复 [英] Spring Integration: Get rid of code duplication for setting up beans

查看:40
本文介绍了Spring 集成:摆脱设置 bean 的代码重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的 SFTP 客户端项目,我使用的是 spring 集成.我们有不同的客户端,必须连接到不同的 SFTP 服务器,但是,所有逻辑都是相同的,所以我将它们抽象为 AbstractSFTPEndPoint.每个特定于客户端的类都实现了 getClientId(),AbstractSFTPEndPoint 使用它来获取特定于客户端的详细信息,例如 SFTP 凭据.

For my SFTP client project, I am using spring integration. We have different clients and have to connect to different SFTP servers, but, all of the logic is same, so I have abstracted them out into AbstractSFTPEndPoint. Each client-specific class implements getClientId(), which is used by AbstractSFTPEndPoint to get client-specific details like SFTP credentials.

然而,所有客户端的整个逻辑都是相同的,但我仍然必须为每个客户端实现特定的类.这主要是因为我们需要为每个客户端单独的MessageSource".

However, the entire logic is same for all the clients, but I am still having to implement specific classes for each client. This is mainly because we need separate "MessageSource" for each client.

我怎样才能摆脱这种重复?

How can I get rid of this duplication?

public class SFTPEndPointForClientAAAA extends AbstractSFTPEndPoint {

    public String getClientId(){
       return "clientAAAA";
    }

    @Bean(name = "channelForClientAAAA")
    public QueueChannel inputFileChannel() {
        return super.inputFileChannel();
    }

    @ServiceActivator(inputChannel = "channelForClientAAAA", poller = @Poller(fixedDelay = "500"))
    public void serviceActivator(Message message) {
        super.serviceActivator(message);
    }

    @Bean(name = "messageSourceForClientAAAA")
    @InboundChannelAdapter(value = "channelForClientAAAA",
            poller = @Poller(fixedDelay = "50", maxMessagesPerPoll = "2"))
    public MessageSource messageSource() {
        return super.messageSource();
    }
}

基本上我有一堆 SFTP 主机要连接并应用相同的逻辑.我希望这可以自动完成,而不必为每个 SFTP 主机实现类.

Basically I have a bunch of SFTP hosts to connect to and apply same logic. I want that to be done automatically without having to implement class for each SFTP host.

推荐答案

参见 动态 ftp 示例.它使用 XML,但同样的技术也适用于 Java 配置.它使用出站适配器;入站稍微复杂一些,因为您可能需要将它们挂钩到一个公共上下文中.自述文件中提供了有关如何执行此操作的链接.

See the dynamic ftp sample. It uses XML but the same techniques apply to Java configuration. It uses outbound adapters; inbound are a little more complicated because you might need to hook them into a common context. There are links in the readme for how to do that.

但是,我最近回答了一个使用 Java 配置的多个 IMAP 邮件适配器的类似问题 然后是后续问题.

However, I recently answered a similar question for multiple IMAP mail adapters using Java configuration and then a follow-up question.

您应该能够使用那里使用的技术.

You should be able to use the technique used there.

这篇关于Spring 集成:摆脱设置 bean 的代码重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆