首页 > 解决方案 > 将 Spring xml 配置转换为 java

问题描述

我在春季有一个用于邮件服务的 XML 配置,我想将其转换为 JAVA 配置。

XML 看起来像这样

<int:chain id="chain"
           input-channel="outMailError"
    output-channel="outMailEntry">
    <int:poller max-messages-per-poll="1" fixed-rate="20000" />
    <int:transformer ref="mailSendErrorTransformer" />
</int:chain>

<int:channel id="outMailError">
    <int:queue capacity="500" />
</int:channel>

<int:channel id="outboundMailEntry" />

我能够将频道转换为

@Bean
public DirectChannel outboundMailEntry() {
    return new DirectChannel();
}

@Bean
public QueueChannel outboundMailErrorChannel() {
    return new QueueChannel(500);
}

但我不知道如何为 int:chain 做同样的事情。我能够调试并找出 spring 为 xml 的“链”部分实例化什么类型的 bean - 它PollingConsumer需要 2 个参数PollableChannel inputChannel, MessageHandler handler

第一个不是问题,因为我已经有了,它是

@Qualifier("outMailError")
QueueChannel channel

但我不知道第二个... Spring 本身初始化了一些MessageHandlerChain,但我无法设置outMailEntry它,也不知道轮询器和变压器.. 有什么想法吗?

标签: javaspringspring-integration

解决方案


chain Java Config中没有等价物。它是专为 XML 设计的,可以最大限度地减少 XML 编码。

另一方面,看起来你根本不需要那个<chain>:你那里只有一个<int:transformer>

在 Java Config 中,您将在方法上使用具有适当和属性的@Trasnoformer注释。等价物也在那里作为具有切除配置的属性存在。mailSendErrorTransformerinputChanneloutputChannel<int:poller>poller@Poller

从这里开始查看文档中的更多信息: https ://docs.spring.io/spring-integration/docs/current/reference/html/overview.html#configuration-enable-integration


推荐阅读