Java DSL
Spring Integration Java 配置和 DSL 提供了一组便利的生成器和流畅的 API,让你能够从 Spring @Configuration
类中配置 Spring Integration 消息流。
The Spring Integration Java configuration and DSL provides a set of convenient builders and a fluent API that lets you configure Spring Integration message flows from Spring @Configuration
classes.
(另见 Kotlin DSL。)
(See also Kotlin DSL.)
(另见 Groovy DSL。)
(See also Groovy DSL.)
针对 Spring Integration 的 Java DSL 实质上是 Spring Integration 的一个门面。DSL 提供了一种简单方法,让你能够使用流畅的 Builder
模式以及 Spring Framework 和 Spring Integration 中的现有 Java 配置,将 Spring Integration 消息流嵌入到你的应用程序中。我们还使用并支持 lambda(Java 8 中可用)进一步简化 Java 配置。
The Java DSL for Spring Integration is essentially a facade for Spring Integration.
The DSL provides a simple way to embed Spring Integration Message Flows into your application by using the fluent Builder
pattern together with existing Java configuration from Spring Framework and Spring Integration.
We also use and support lambdas (available with Java 8) to further simplify Java configuration.
cafe提供了一个很好的 DSL 使用示例。
The cafe offers a good example of using the DSL.
DSL 由流畅的 IntegrationFlow
API 提供(请参阅 IntegrationFlowBuilder
)。这将生成 IntegrationFlow
组件,该组件应注册为 Spring bean(通过使用 @Bean
注解)。生成器模式用于将任意复杂结构表示为接受 lambda 作为参数的方法层次结构。
The DSL is presented by the IntegrationFlow
fluent API (see IntegrationFlowBuilder
).
This produces the IntegrationFlow
component, which should be registered as a Spring bean (by using the @Bean
annotation).
The builder pattern is used to express arbitrarily complex structures as a hierarchy of methods that can accept lambdas as arguments.
IntegrationFlowBuilder
仅收集 IntegrationFlow
bean 中的集成组件(如 MessageChannel
实例和 AbstractEndpoint
实例等),以便由 IntegrationFlowBeanPostProcessor
进一步解析并注册应用程序上下文中的具体 bean。
The IntegrationFlowBuilder
only collects integration components (MessageChannel
instances, AbstractEndpoint
instances, and so on) in the IntegrationFlow
bean for further parsing and registration of concrete beans in the application context by the IntegrationFlowBeanPostProcessor
.
Java DSL 直接使用 Spring Integration 类,并绕过任何 XML 生成和解析。但是,DSL 提供的不仅仅是在 XML 之上的语法糖。它最引人注目的功能之一是能够定义内联 lambda 来实现端点逻辑,无需使用外部类来实现自定义逻辑。在某种意义上,Spring Integration 对 Spring 表达式语言 (SpEL) 和内联脚本的支持解决了这个问题,但 lambda 更容易且功能更强大。
The Java DSL uses Spring Integration classes directly and bypasses any XML generation and parsing. However, the DSL offers more than syntactic sugar on top of XML. One of its most compelling features is the ability to define inline lambdas to implement endpoint logic, eliminating the need for external classes to implement custom logic. In some sense, Spring Integration’s support for the Spring Expression Language (SpEL) and inline scripting address this, but lambdas are easier and much more powerful.
以下示例展示了如何对 Spring Integration 使用 Java 配置:
The following example shows how to use Java Configuration for Spring Integration:
@Configuration
@EnableIntegration
public class MyConfiguration {
@Bean
public AtomicInteger integerSource() {
return new AtomicInteger();
}
@Bean
public IntegrationFlow myFlow(AtomicInteger integerSource) {
return IntegrationFlow.fromSupplier(integerSource::getAndIncrement,
c -> c.poller(Pollers.fixedRate(100)))
.channel("inputChannel")
.filter((Integer p) -> p > 0)
.transform(Object::toString)
.channel(MessageChannels.queue())
.get();
}
}
前述配置示例的结果是在 ApplicationContext
启动后创建 Spring Integration 端点和消息通道。Java 配置既可用来替换 XML 配置,又可用来增强 XML 配置。你无需替换现有的所有 XML 配置即可使用 Java 配置。
The result of the preceding configuration example is that it creates, after ApplicationContext
start up, Spring Integration endpoints and message channels.
Java configuration can be used both to replace and augment XML configuration.
You need not replace all of your existing XML configuration to use Java configuration.