Groovy 支持
在 Spring Integration 2.0 中,我们添加了 Groovy 支持,允许您使用 Groovy 脚本语言为各种集成组件提供逻辑 — 类似于 Spring Expression Language (SpEL) 对路由、转换和其他集成方面的支持。有关 Groovy 的更多信息,请参阅 Groovy 文档,您可以在其项目网站上找到。
您需要在项目中包含此依赖项
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-groovy</artifactId>
<version>6.4.4</version>
</dependency>
compile "org.springframework.integration:spring-integration-groovy:6.4.4"
此外,从 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 组件,指定应将 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 选项没有任何影响,并且可重新加载的脚本也可以静态编译。 |