Groovy 支持
在 Spring Integration 2.0 中,我们添加了对 Groovy 的支持,允许您使用 Groovy 脚本语言为各种集成组件提供逻辑 — — 类似于 Spring 表达式语言 (SpEL) 在路由、转换和其他集成问题中的支持方式。有关 Groovy 的更多信息,请参阅 Groovy 文档,您可以在 项目网站 上找到该文档。
你需要将这个依赖添加到你的项目中:
- Maven
- Gradle
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-groovy</artifactId>
<version>6.4.2</version>
</dependency>
compile "org.springframework.integration:spring-integration-groovy:6.4.2"
此外,从 6.0 版开始,提供了一个 Groovy DSL 用于集成流配置。
Groovy 配置
从 Spring Integration 2.1 开始,Groovy 支持的配置命名空间是 Spring Integration 脚本支持的扩展,并且共享在 脚本支持 部分详细描述的核心配置和行为。即使 Groovy 脚本得到了通用脚本支持的良好支持,Groovy 支持还是提供了 Groovy
配置命名空间,它由 Spring 框架的 org.springframework.scripting.groovy.GroovyScriptFactory
和相关组件支持,为使用 Groovy 提供了扩展功能。以下列表显示了两个示例配置:
<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
属性在这个命名空间中无效。
Groovy 对象自定义
如果你需要自定义 Groovy 对象本身(而不仅仅是设置变量),你可以通过使用 customizer
属性引用一个实现了 GroovyObjectCustomizer
的 bean。例如,这在你想通过修改 MetaClass
并注册函数以在脚本内可用的方式来实现领域特定语言(DSL)时可能会很有用。以下示例展示了如何做到这一点:
<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 Integration 3.0 引入了 variables
属性,它与 variable
元素配合使用。此外,Groovy 脚本具有将变量解析为 BeanFactory
中的 bean 的能力,如果未提供同名的绑定变量。以下示例展示了如何使用变量 (entityManager
) :
<int-groovy:script>
<![CDATA[
entityManager.persist(payload)
payload
]]>
</int-groovy:script>
entityManager
必须是应用程序上下文中的适当 bean。
有关 <variable>
元素、variables
属性和 script-variable-generator
属性的更多信息,请参阅脚本变量绑定。
Groovy 脚本编译器定制
@CompileStatic
提示是使用最广泛的 Groovy 编译器自定义选项。它可以在类或方法级别上使用。有关更多信息,请参阅 Groovy 参考手册 和,特别是,@CompileStatic。为了在集成场景中为短脚本利用此功能,我们被迫将简单的脚本更改为更像 Java 的代码。考虑以下 <filter>
脚本:
headers.type == 'good'
前面的脚本在 Spring Integration 中变为以下方法:
@groovy.transform.CompileStatic
String filter(Map headers) {
headers.type == 'good'
}
filter(headers)
这样,filter()
方法被转换并编译为静态 Java 代码,绕过了 Groovy 的动态调用阶段,例如 getProperty()
工厂和 CallSite
代理。
从 4.3 版本开始,您可以使用 compile-static
boolean
选项配置 Spring Integration Groovy 组件,指定应将 ASTTransformationCustomizer
(用于 @CompileStatic
)添加到内部 CompilerConfiguration
。这样设置后,您可以在脚本代码中省略带有 @CompileStatic
的方法声明,但仍能获得编译后的纯 Java 代码。在这种情况下,前面的脚本可以更短,但仍然需要比解释型脚本稍微冗长一些,如下例所示:
binding.variables.headers.type == 'good'
你必须通过 groovy.lang.Script
的 binding
属性来访问 headers
和 payload
(或其他任何)变量,因为使用 @CompileStatic
时,我们没有动态的 GroovyObject.getProperty()
功能。
此外,我们引入了 compiler-configuration
bean 引用。通过这个属性,你可以提供任何其他所需的 Groovy 编译器自定义设置,例如 ImportCustomizer
。有关此功能的更多信息,请参阅 Groovy 文档中的高级编译器配置。
使用 compilerConfiguration
不会自动为 @CompileStatic
注解添加一个 ASTTransformationCustomizer
,并且它会覆盖 compileStatic
选项。如果你仍然需要 CompileStatic
,你应该手动将 new ASTTransformationCustomizer(CompileStatic.class)
添加到该自定义 compilerConfiguration
的 CompilationCustomizers
中。
Groovy 编译器定制对 refresh-check-delay
选项没有影响,可重载脚本也可以进行静态编译。