有关 SpEL 支持的详细信息_自定义函数

您可以使用用 Spring 表达式语言编写的表达式来配置许多 Spring 集成组件。

在大多数情况下,对象是 ,它有两个属性 ( 和 ),允许使用 、 、 等表达式。​​#root​​​​Message​​​​headers​​​​payload​​​​payload​​​​payload.thing​​​​headers['my.header']​

在某些情况下,会提供其他变量。 例如,提供(来自 HTTP 请求的参数)和(来自 URI 中路径占位符的值)。​​<int-http:inbound-gateway/>​​​​#requestParams​​​​#pathVariables​

对于所有 SpEL 表达式,a 可用于启用对应用程序上下文中任何 Bean 的引用(例如,)。 此外,还有两个可用。 A 允许使用键访问 a 中的值,允许访问字段和 JavaBean 兼容属性(通过使用 getter 和 setter)。 这是访问标头和有效负载属性的方式。​​BeanResolver​​​​@myBean.foo(payload)​​​​PropertyAccessors​​​​MapAccessor​​​​Map​​​​ReflectivePropertyAccessor​​​​Message​

SpEL 评估上下文定制

从 Spring Integration 3.0 开始,您可以将其他实例添加到框架使用的 SpEL 评估上下文中。 该框架提供(只读),您可以使用它访问 中的字段 或 JSON。 如果您有特定需求,也可以创建自己的。​​PropertyAccessor​​​​JsonPropertyAccessor​​​​JsonNode​​​​String​​​​PropertyAccessor​

此外,您还可以添加自定义函数。 自定义函数是在类上声明的方法。 函数和属性访问器在整个框架中使用的任何 SpEL 表达式中都可用。​​static​

以下配置演示如何直接配置 with 自定义属性访问器和函数:​​IntegrationEvaluationContextFactoryBean​

<bean id="integrationEvaluationContext"
class="org.springframework.integration.config.IntegrationEvaluationContextFactoryBean">
<property name="propertyAccessors">
<util:map>
<entry key="things">
<bean class="things.MyCustomPropertyAccessor"/>
</entry>
</util:map>
</property>
<property name="functions">
<map>
<entry key="barcalc" value="#{T(things.MyFunctions).getMethod('calc', T(things.MyThing))}"/>
</map>
</property>
</bean>

为方便起见,Spring 集成为属性访问器和函数提供了命名空间支持,如以下各节所述。 框架会自动代表您配置工厂 Bean。

此工厂 Bean 定义将覆盖缺省 Bean 定义。 它将自定义访问器和一个自定义函数添加到列表中(其中还包括前面提到的标准访问器)。​​integrationEvaluationContext​

请注意,自定义函数是静态方法。 在前面的示例中,自定义函数是在调用的类上调用的静态方法,并采用 的单个类型参数。​​calc​​​​MyFunctions​​​​MyThing​

假设您有一个有效负载,其类型为 . 此外,假设您需要执行一些操作来创建调用 from 的对象,然后调用在该对象上调用的自定义函数。​​Message​​​​MyThing​​​​MyObject​​​​MyThing​​​​calc​

标准属性访问器不知道如何从 获取 ,因此您可以编写和配置自定义属性访问器来执行此操作。 因此,您的最终表达式可能是 。​​MyObject​​​​MyThing​​​​"#barcalc(payload.myObject)"​

工厂 Bean 具有另一个属性 (),该属性允许您自定义在 SpEL 评估期间使用的属性。 您可能需要在使用非标准 . 在以下示例中,SpEL 表达式始终使用 Bean 工厂的类装入器:​​typeLocator​​​​TypeLocator​​​​ClassLoader​

<bean id="integrationEvaluationContext"
class="org.springframework.integration.config.IntegrationEvaluationContextFactoryBean">
<property name="typeLocator">
<bean class="org.springframework.expression.spel.support.StandardTypeLocator">
<constructor-arg value="#{beanFactory.beanClassLoader}"/>
</bean>
</property>
</bean>

SpEL 函数

Spring 集成提供了命名空间支持,允许您创建 SpEL 自定义函数。 您可以指定组件以向整个框架中使用的 提供自定义 SpEL 函数。 您可以添加一个或多个这些组件,而不是配置前面显示的工厂 Bean,框架会自动将它们添加到缺省工厂 Bean。​​<spel-function/>​​​​EvaluationContext​​​​integrationEvaluationContext​

例如,假设您有一个有用的静态方法来计算 XPath。 下面的示例演示如何创建自定义函数以使用该方法:

<int:spel-function id="xpath"
class="com.something.test.XPathUtils" method="evaluate(java.lang.String, java.lang.Object)"/>

<int:transformer input-channel="in" output-channel="out"
expression="#xpath('//things/@mythings', payload)" />

给定前面的示例:

  • ID 为 的缺省 Bean 已注册到应用程序上下文中。IntegrationEvaluationContextFactoryBeanintegrationEvaluationContext
  • 解析并添加到 of 作为映射条目,其作为键,静态作为值。<spel-function/>functionsMapintegrationEvaluationContextidMethod
  • 工厂 Bean 创建一个新实例,并配置了缺省实例 a 和定制函数。integrationEvaluationContextStandardEvaluationContextPropertyAccessorBeanResolver
  • 该实例被注入到 Bean 中。EvaluationContextExpressionEvaluatingTransformer

要使用 Java 配置提供 SpEL 函数,您可以为每个函数声明一个 bean。 以下示例演示如何创建自定义函数:​​SpelFunctionFactoryBean​

@Bean
public SpelFunctionFactoryBean xpath() {
return new SpelFunctionFactoryBean(XPathUtils.class, "evaluate");
}

在父上下文中声明的 SpEL 函数也可以在任何子上下文中使用。 每个上下文都有自己的工厂 Bean 实例,因为每个上下文都需要不同的 ,但函数声明是继承的,可以通过声明具有相同名称的 SpEL 函数来覆盖。​​integrationEvaluationContext​​​​BeanResolver​

内置 SpEL 函数

Spring 集成提供了以下标准函数,这些函数在启动时自动注册到应用程序上下文中:

  • ​#jsonPath​​:计算指定对象上的“jsonPath”。 此函数调用 ,委托给 Jayway JsonPath 库。 以下清单显示了一些用法示例:JsonPathUtils.evaluate(…​)
<transformer expression="#jsonPath(payload, '$.store.book[0].author')"/>

<filter expression="#jsonPath(payload,'$..book[2].isbn') matches '\d-\d{3}-\d{5}-\d'"/>

<splitter expression="#jsonPath(payload, '$.store.book')"/>

<router expression="#jsonPath(payload, headers.jsonPath)">
<mapping channel="output1" value="reference"/>
<mapping channel="output2" value="fiction"/>
</router>

#jsonPath还支持第三个(可选)参数:com.jayway.jsonpath.Filter 数组,可以通过引用 bean 或 bean 方法(例如)来提供。

使用此函数需要 Jayway JsonPath 库 () 位于类路径上。 否则,不会注册 SpEL 函数。​​json-path.jar​​​​#jsonPath​

有关 JSON 的详细信息,请参阅转换器中的“JSON 转换器”。

  • #xpath:评估某些提供的对象的“xpath”。 有关 XML 和 XPath 的详细信息,请参阅 XML 支持 - 处理 XML 有效负载。

属性访问器

Spring Integration 提供了命名空间支持,允许您创建 SpEL 自定义 PropertyAccessor 实现。 您可以使用该组件为整个框架中使用的 提供自定义实例的列表。 您可以添加一个或多个这些组件,而不是配置前面显示的工厂 Bean,框架会自动将访问器添加到缺省工厂 Bean。 以下示例演示如何执行此操作:​​<spel-property-accessors/>​​​​PropertyAccessor​​​​EvaluationContext​​​​integrationEvaluationContext​

<int:spel-property-accessors>
<bean id="jsonPA" class="org.springframework.integration.json.JsonPropertyAccessor"/>
<ref bean="fooPropertyAccessor"/>
</int:spel-property-accessors>

在前面的示例中,将两个自定义实例注入到 (按声明它们的顺序)。​​PropertyAccessor​​​​EvaluationContext​

要使用 Java 配置提供实例,您应该声明一个名称为 (由常量指示)的 Bean。 以下示例演示如何使用 Java 配置两个自定义实例:​​PropertyAccessor​​​​SpelPropertyAccessorRegistrar​​​​spelPropertyAccessorRegistrar​​​​IntegrationContextUtils.SPEL_PROPERTY_ACCESSOR_REGISTRAR_BEAN_NAME​​​​PropertyAccessor​

@Bean
public SpelPropertyAccessorRegistrar spelPropertyAccessorRegistrar() {
return new SpelPropertyAccessorRegistrar(new JsonPropertyAccessor())
.add(fooPropertyAccessor());
}


在父上下文中声明的自定义实例也可以在任何子上下文中使用。 它们位于结果列表的末尾(但在默认值和 之前)。 如果在子上下文中声明具有相同 Bean ID 的 ,它将覆盖父访问器。 在 中声明的 Bean 必须具有“id”属性。 最终使用顺序如下:​​PropertyAccessor​​​​org.springframework.context.expression.MapAccessor​​​​o.s.expression.spel.support.ReflectivePropertyAccessor​​​​PropertyAccessor​​​​<spel-property-accessors/>​



  • 当前上下文中的访问器,按声明顺序排列
  • 父上下文中的任何访问器,按顺序
  • 这​​MapAccessor​
  • 这​​ReflectivePropertyAccessor​