对于最新的稳定版本,请使用 Spring Integration 6.4.3spring-doc.cadn.net.cn

Groovy 支持

在 Spring Integration 2.0 中,我们添加了 Groovy 支持,允许您使用 Groovy 脚本语言为各种集成组件提供逻辑,类似于 Spring 表达式语言 (SPEL) 支持路由、转换和其他集成问题的方式。 有关 Groovy 的更多信息,请参阅 Groovy 文档,您可以在项目网站上找到该文档。spring-doc.cadn.net.cn

您需要将此依赖项包含在您的项目中:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-groovy</artifactId>
    <version>6.3.9</version>
</dependency>
compile "org.springframework.integration:spring-integration-groovy:6.3.9"

此外,从 V6.0 开始,提供了用于集成流配置的 Groovy DSLspring-doc.cadn.net.cn

Groovy 配置

在 Spring Integration 2.1 中,Groovy 支持的配置名称空间是 Spring Integration 的脚本支持的扩展,并共享脚本支持部分中详细描述的核心配置和行为。 尽管通用脚本支持很好地支持 Groovy 脚本,但 Groovy 支持提供了Groovyconfiguration 命名空间,它由 Spring Framework 的org.springframework.scripting.groovy.GroovyScriptFactory和相关组件,为使用 Groovy 提供扩展功能。 下面的清单显示了两个示例配置:spring-doc.cadn.net.cn

Filter
<int:filter input-channel="referencedScriptInput">
   <int-groovy:script location="some/path/to/groovy/file/GroovyFilterTests.groovy"/>
</int:filter>

<int:filter input-channel="inlineScriptInput">
     <int-groovy:script><![CDATA[
     return payload == 'good'
   ]]></int-groovy:script>
</int:filter>

如前面的示例所示,该配置看起来与常规脚本支持配置相同。 唯一的区别是使用 Groovy 命名空间,如int-groovynamespace 前缀。 另请注意,lang属性<script>标记在此命名空间中无效。spring-doc.cadn.net.cn

Groovy 对象自定义

如果需要自定义 Groovy 对象本身(除了设置变量之外),则可以引用实现GroovyObjectCustomizer通过使用customizer属性。 例如,如果您想通过修改MetaClass以及注册要在脚本中可用的函数。 以下示例显示了如何执行此作:spring-doc.cadn.net.cn

<int:service-activator input-channel="groovyChannel">
    <int-groovy:script location="somewhere/SomeScript.groovy" customizer="groovyCustomizer"/>
</int:service-activator>

<beans:bean id="groovyCustomizer" class="org.something.MyGroovyObjectCustomizer"/>

设置自定义GroovyObjectCustomizer与 不互斥<variable>元素或script-variable-generator属性。 也可以在定义内联脚本时提供。spring-doc.cadn.net.cn

Spring Integration 3.0 引入了variables属性,该属性与variable元素。 此外,groovy 脚本能够将变量解析为 Bean 中的BeanFactory,如果 name 中没有提供绑定变量。 以下示例演示如何使用变量 (entityManager):spring-doc.cadn.net.cn

<int-groovy:script>
    <![CDATA[
        entityManager.persist(payload)
        payload
    ]]>
</int-groovy:script>

entityManager必须是应用程序上下文中的适当 Bean。spring-doc.cadn.net.cn

有关<variable>元素中,variables属性和script-variable-generator属性,请参阅脚本变量绑定spring-doc.cadn.net.cn

Groovy Script 编译器定制

@CompileStatichint 是最流行的 Groovy 编译器自定义选项。 它可以在类或方法级别使用。 有关更多信息,请参阅 Groovy 参考手册,特别是 @CompileStatic。 为了将此功能用于短脚本(在集成场景中),我们被迫将简单脚本更改为更类似于 Java 的代码。 请考虑以下<filter>脚本:spring-doc.cadn.net.cn

headers.type == 'good'

前面的脚本在 Spring Integration 中成为以下方法:spring-doc.cadn.net.cn

@groovy.transform.CompileStatic
String filter(Map headers) {
	headers.type == 'good'
}

filter(headers)

这样,filter()方法被转换并编译为静态 Java 代码,绕过 Groovy 调用的动态阶段,例如getProperty()factories 和CallSite代理。spring-doc.cadn.net.cn

从版本 4.3 开始,您可以使用compile-static boolean选项,指定ASTTransformationCustomizer@CompileStatic应该添加到内部CompilerConfiguration. 有了这个,你可以省略方法声明@CompileStatic,并且仍然获得编译后的纯 Java 代码。 在这种情况下,前面的脚本可以很短,但仍需要比解释的脚本更详细一些,如下例所示:spring-doc.cadn.net.cn

binding.variables.headers.type == 'good'

您必须访问headerspayload(或任何其他)变量通过groovy.lang.Script bindingproperty 的 b,因为@CompileStatic,我们没有动态GroovyObject.getProperty()能力。spring-doc.cadn.net.cn

此外,我们还引入了compiler-configurationbean 引用。 使用此属性,您可以提供任何其他必需的 Groovy 编译器自定义,例如ImportCustomizer. 有关此功能的更多信息,请参阅高级编译器配置的 Groovy 文档。spring-doc.cadn.net.cn

compilerConfiguration不会自动添加ASTTransformationCustomizer对于@CompileStatic注解,它会覆盖compileStatic选择。 如果您仍然需要CompileStatic中,您应该手动添加new ASTTransformationCustomizer(CompileStatic.class)CompilationCustomizers那个习俗compilerConfiguration.
Groovy 编译器自定义对refresh-check-delay选项和可重新加载的脚本也可以静态编译。

控制总线

如 (企业集成模式) 中所述,控制总线背后的思想是,您可以使用与 “应用程序级” 消息传递相同的消息传递系统来监视和管理框架内的组件。 在 Spring 集成中,我们构建在前面描述的适配器之上,以便您可以发送消息作为调用公开作的一种方式。 这些作的一个选项是 Groovy 脚本。 下面的示例为 control bus 配置一个 Groovy 脚本:spring-doc.cadn.net.cn

<int-groovy:control-bus input-channel="operationChannel"/>

控制总线有一个 Importing 通道,可以访问该通道以调用应用程序上下文中 bean 上的作。spring-doc.cadn.net.cn

Groovy 控制总线将 Importing 通道上的消息作为 Groovy 脚本运行。 它接受一条消息,将正文编译为脚本,并使用GroovyObjectCustomizer,然后运行它。 控制总线的MessageProcessor公开应用程序上下文中所有带有 Comments 的 bean@ManagedResource并实现 Spring 的Lifecycle接口或扩展 Spring 的CustomizableThreadCreator基类(例如,多个TaskExecutorTaskSchedulerimplementations) 的 Implementations)。spring-doc.cadn.net.cn

在 Control Bus' 命令脚本中使用具有自定义作用域(例如 'request')的托管 bean 时要小心,尤其是在异步消息流中。 如果MessageProcessor的控制总线无法从应用程序上下文中公开一个 bean,你最终可能会得到一些BeansException在命令脚本运行期间。 例如,如果未建立自定义范围的上下文,则尝试获取该范围内的 bean 会触发BeanCreationException.

如果需要进一步自定义 Groovy 对象,还可以提供对实现GroovyObjectCustomizer通过customizer属性,如下例所示:spring-doc.cadn.net.cn

<int-groovy:control-bus input-channel="input"
        output-channel="output"
        customizer="groovyCustomizer"/>

<beans:bean id="groovyCustomizer" class="org.foo.MyGroovyObjectCustomizer"/>