如需使用最新稳定版本,请使用 Spring Integration 7.0.4spring-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.4.10</version>
</dependency>
compile "org.springframework.integration:spring-integration-groovy:6.4.10"

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

Groovy 配置

使用 Spring Integration 2.1,Groovy 支持的配置命名空间是 Spring Integration 脚本支持的一个扩展,并共享脚本支持部分中详细描述的核心配置和行为。 尽管通用脚本支持对 Groovy 脚本提供了良好的支持,但 Groovy 支持还提供了由 Spring Framework 的Groovy及相关组件支持的org.springframework.scripting.groovy.GroovyScriptFactory配置命名空间,为使用 Groovy 提供了扩展功能。 以下列表展示了两个示例配置:spring-doc.cadn.net.cn

过滤器
<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-groovy 命名空间前缀所示。 另外请注意,<script> 标签上的 lang 属性在此命名空间中无效。spring-doc.cadn.net.cn

Groovy 对象自定义

如果您需要自定义 Groovy 对象本身(而不仅仅是设置变量),可以通过使用 GroovyObjectCustomizer 属性引用实现了 customizer 的 bean。 例如,如果您想通过修改 MetaClass 并注册在脚本中可用的函数来实现领域特定语言(DSL),这可能非常有用。 以下示例展示了如何实现这一点: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 脚本能够将变量解析为 BeanFactory 中的 Bean。 以下示例展示了如何使用变量(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 脚本编译器自定义

@CompileStatic 提示是最受欢迎的 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() 工厂和 CallSite 代理。spring-doc.cadn.net.cn

从 4.3 版本开始,您可以使用 compile-static boolean 选项配置 Spring Integration Groovy 组件,指定为 ASTTransformationCustomizer 添加 @CompileStatic 到内部 CompilerConfiguration。 在此基础上,您可以在脚本代码中省略带有 @CompileStatic 的方法声明,同时仍能获得编译后的纯 Java 代码。 在这种情况下,上述脚本可以简短,但仍需比解释型脚本稍显冗长,如下示例所示:spring-doc.cadn.net.cn

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

您必须通过 groovy.lang.Script binding 属性访问 headerspayload(或任何其他)变量,因为使用 @CompileStatic 时,我们不具备动态 GroovyObject.getProperty() 功能。spring-doc.cadn.net.cn

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

使用 compilerConfiguration 不会自动为 @CompileStatic 注解添加 ASTTransformationCustomizer,并且它会覆盖 compileStatic 选项。 如果您仍然需要 CompileStatic,则应手动将该自定义 compilerConfigurationCompilationCustomizers 中添加一个 new ASTTransformationCustomizer(CompileStatic.class)
Groovy 编译器自定义设置对 refresh-check-delay 选项没有任何影响,可重新加载的脚本也可以进行静态编译。