到目前为止显示的所有示例都说明了 DSL 如何使用 Spring Integration 编程模型来支持消息体系结构。 但是,我们还没有进行任何真正的整合。 这样做需要通过 HTTP、JMS、AMQP、TCP、JDBC、FTP、SMTP 等访问远程资源,或者访问本地文件系统。 Spring Integration 支持所有这些以及更多功能。 理想情况下,DSL 应该为所有这些提供一流的支持,但实现所有这些并跟上 Spring Integration 中新适配器的步伐是一项艰巨的任务。 因此,人们期望DSL不断赶上Spring Integration。Spring中文文档

因此,我们提供了高级 API 来无缝定义特定于协议的消息传递。 我们使用 factory 和 builder 模式以及 lambda 来做到这一点。 您可以将工厂类视为“命名空间工厂”,因为它们与特定于具体协议的 Spring Integration 模块中的组件的 XML 命名空间扮演相同的角色。 目前,Spring Integration Java DSL 支持 、 、 和 命名空间工厂。 以下示例演示如何使用其中三个 (、 和 ):AmqpFeedJmsFiles(S)FtpHttpJPAMongoDbTCP/UDPMailWebFluxScriptsAmqpJmsMailSpring中文文档

@Bean
public IntegrationFlow amqpFlow() {
    return IntegrationFlow.from(Amqp.inboundGateway(this.rabbitConnectionFactory, queue()))
            .transform("hello "::concat)
            .transform(String.class, String::toUpperCase)
            .get();
}

@Bean
public IntegrationFlow jmsOutboundGatewayFlow() {
    return IntegrationFlow.from("jmsOutboundGatewayChannel")
            .handle(Jms.outboundGateway(this.jmsConnectionFactory)
                        .replyContainer(c ->
                                    c.concurrentConsumers(3)
                                            .sessionTransacted(true))
                        .requestDestination("jmsPipelineTest"))
            .get();
}

@Bean
public IntegrationFlow sendMailFlow() {
    return IntegrationFlow.from("sendMailChannel")
            .handle(Mail.outboundAdapter("localhost")
                            .port(smtpPort)
                            .credentials("user", "pw")
                            .protocol("smtp")
                            .javaMailProperties(p -> p.put("mail.debug", "true")),
                    e -> e.id("sendMailEndpoint"))
            .get();
}

前面的示例演示如何使用“命名空间工厂”作为内联适配器声明。 但是,您可以在定义中使用它们,以使方法链更具可读性。@BeanIntegrationFlowSpring中文文档

我们正在征求社区对这些命名空间工厂的反馈,然后再将精力投入到其他工厂上。 我们也感谢任何关于我们接下来应该支持哪些适配器和网关的优先级的意见。
我们正在征求社区对这些命名空间工厂的反馈,然后再将精力投入到其他工厂上。 我们也感谢任何关于我们接下来应该支持哪些适配器和网关的优先级的意见。

您可以在本参考手册中特定于协议的章节中找到更多 Java DSL 示例。Spring中文文档

所有其他协议通道适配器都可以配置为通用 Bean 并连接到 ,如以下示例所示:IntegrationFlowSpring中文文档

@Bean
public QueueChannelSpec wrongMessagesChannel() {
    return MessageChannels
            .queue()
            .wireTap("wrongMessagesWireTapChannel");
}

@Bean
public IntegrationFlow xpathFlow(MessageChannel wrongMessagesChannel) {
    return IntegrationFlow.from("inputChannel")
            .filter(new StringValueTestXPathMessageSelector("namespace-uri(/*)", "my:namespace"),
                    e -> e.discardChannel(wrongMessagesChannel))
            .log(LoggingHandler.Level.ERROR, "test.category", m -> m.getHeaders().getId())
            .route(xpathRouter(wrongMessagesChannel))
            .get();
}

@Bean
public AbstractMappingMessageRouter xpathRouter(MessageChannel wrongMessagesChannel) {
    XPathRouter router = new XPathRouter("local-name(/*)");
    router.setEvaluateAsString(true);
    router.setResolutionRequired(false);
    router.setDefaultOutputChannel(wrongMessagesChannel);
    router.setChannelMapping("Tags", "splittingChannel");
    router.setChannelMapping("Tag", "receivedChannel");
    return router;
}