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.0</version>
</dependency>
compile "org.springframework.integration:spring-integration-groovy:7.0.0"

此外,从版本 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属性访问headerspayload(或任何其他)变量,因为使用@CompileStatic,我们没有动态GroovyObject.getProperty()功能。

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

使用compilerConfiguration不会自动为@CompileStatic注解添加ASTTransformationCustomizer,并且它会覆盖compileStatic选项。如果您仍然需要CompileStatic,您应该手动将new ASTTransformationCustomizer(CompileStatic.class)添加到该自定义compilerConfigurationCompilationCustomizers中。
Groovy 编译器自定义对refresh-check-delay选项没有任何影响,并且可重载脚本也可以静态编译。
© . This site is unofficial and not affiliated with VMware.