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>7.0.2</version>
</dependency>
compile "org.springframework.integration:spring-integration-groovy:7.0.2"
此外,从 6.0 版本开始,还提供了用于集成流配置的 Groovy DSL。
Groovy 配置
自 Spring Integration 2.1 起,Groovy 支持的配置命名空间是 Spring Integration 脚本支持的一个扩展,并共享脚本支持章节中详细描述的核心配置与行为。尽管通用脚本支持已能很好地支持 Groovy 脚本,但 Groovy 支持提供了 Groovy 配置命名空间,该命名空间基于 Spring Framework 的 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 组件,指定应将 @CompileStatic 的 ASTTransformationCustomizer 添加到内部 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 选项没有任何影响,并且可重载脚本也可以进行静态编译。