此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.4.3! |
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.4-SNAPSHOT</version>
</dependency>
compile "org.springframework.integration:spring-integration-groovy:6.4.4-SNAPSHOT"
此外,从 V6.0 开始,提供了用于集成流配置的 Groovy DSL。
Groovy 配置
在 Spring Integration 2.1 中,Groovy 支持的配置名称空间是 Spring Integration 的脚本支持的扩展,并共享脚本支持部分中详细描述的核心配置和行为。
尽管通用脚本支持很好地支持 Groovy 脚本,但 Groovy 支持提供了Groovy
configuration 命名空间,它由 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
namespace 前缀。
另请注意,lang
属性<script>
标记在此命名空间中无效。
Groovy 对象自定义
如果需要自定义 Groovy 对象本身(除了设置变量之外),则可以引用实现GroovyObjectCustomizer
通过使用customizer
属性。
例如,如果您想通过修改MetaClass
以及注册要在脚本中可用的函数。
以下示例显示了如何执行此作:
<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 脚本能够将变量解析为 Bean 中的BeanFactory
,如果 name 中没有提供绑定变量。
以下示例演示如何使用变量 (entityManager
):
<int-groovy:script>
<![CDATA[
entityManager.persist(payload)
payload
]]>
</int-groovy:script>
entityManager
必须是应用程序上下文中的适当 Bean。
有关<variable>
元素中,variables
属性和script-variable-generator
属性,请参阅脚本变量绑定。
Groovy Script 编译器定制
这@CompileStatic
hint 是最流行的 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()
factories 和CallSite
代理。
从版本 4.3 开始,您可以使用compile-static
boolean
选项,指定ASTTransformationCustomizer
为@CompileStatic
应该添加到内部CompilerConfiguration
.
有了这个,你可以省略方法声明@CompileStatic
,并且仍然获得编译后的纯 Java 代码。
在这种情况下,前面的脚本可以很短,但仍需要比解释的脚本更详细一些,如下例所示:
binding.variables.headers.type == 'good'
您必须访问headers
和payload
(或任何其他)变量通过groovy.lang.Script
binding
property 的 b,因为@CompileStatic
,我们没有动态GroovyObject.getProperty()
能力。
此外,我们还引入了compiler-configuration
bean 引用。
使用此属性,您可以提供任何其他必需的 Groovy 编译器自定义,例如ImportCustomizer
.
有关此功能的更多信息,请参阅高级编译器配置的 Groovy 文档。
用compilerConfiguration 不会自动添加ASTTransformationCustomizer 对于@CompileStatic 注解,它会覆盖compileStatic 选择。
如果您仍然需要CompileStatic 中,您应该手动添加new ASTTransformationCustomizer(CompileStatic.class) 到CompilationCustomizers 那个习俗compilerConfiguration . |
Groovy 编译器自定义对refresh-check-delay 选项和可重新加载的脚本也可以静态编译。 |