此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.3.1Spring中文文档

此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.3.1Spring中文文档

您可以使用用 Spring 表达式语言编写的表达式来配置许多 Spring Integration 组件。Spring中文文档

在大多数情况下,对象是 ,它有两个属性 ( 和 ),允许 、 、 等表达式。#rootMessageheaderspayloadpayloadpayload.thingheaders['my.header']Spring中文文档

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

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

SpEL 评估上下文定制

从 Spring Integration 3.0 开始,您可以向框架使用的 SpEL 评估上下文添加其他实例。 该框架提供了 (只读) ,您可以使用它从 . 如果您有特定需求,也可以创建自己的。PropertyAccessorJsonPropertyAccessorJsonNodeStringPropertyAccessorSpring中文文档

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

以下配置演示如何直接配置自定义属性访问器和函数:IntegrationEvaluationContextFactoryBeanSpring中文文档

<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 Integration 为属性访问器和函数提供命名空间支持,如以下各节所述。 该框架会代表您自动配置工厂 Bean。Spring中文文档

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

请注意,自定义函数是静态方法。 在前面的示例中,自定义函数是在被调用的类上调用的静态方法,它采用 类型的单个参数。calcMyFunctionsMyThingSpring中文文档

假设您的有效负载类型为 . 此外,假设您需要执行一些操作来创建一个调用的对象,然后调用对该对象调用的自定义函数。MessageMyThingMyObjectMyThingcalcSpring中文文档

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

工厂 Bean 具有另一个属性 (),它允许您自定义在 SpEL 评估期间使用的 Bean。 您可能需要在某些使用非标准 . 在下面的示例中,SpEL 表达式始终使用 Bean 工厂的类装入器:typeLocatorTypeLocatorClassLoaderSpring中文文档

<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 Integration 提供命名空间支持,允许您创建 SpEL 自定义函数。 您可以指定组件,以便为整个框架中使用的 SpEL 函数提供自定义 SpEL 函数。 您可以添加一个或多个这些组件,而不是配置前面所示的工厂 Bean,框架会自动将它们添加到缺省工厂 Bean 中。<spel-function/>EvaluationContextintegrationEvaluationContextSpring中文文档

例如,假设您有一个有用的静态方法来评估 XPath。 以下示例演示如何创建自定义函数以使用该方法:Spring中文文档

<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)" />

给定前面的示例:Spring中文文档

  • ID 为 的缺省 Bean 将注册到应用程序上下文中。IntegrationEvaluationContextFactoryBeanintegrationEvaluationContextSpring中文文档

  • 解析并添加为 of 作为映射条目,其中 its 为键,static 为 value。<spel-function/>functionsMapintegrationEvaluationContextidMethodSpring中文文档

  • 工厂 Bean 创建一个新实例,并配置了缺省实例、a 和自定义函数。integrationEvaluationContextStandardEvaluationContextPropertyAccessorBeanResolverSpring中文文档

  • 该实例被注入到 Bean 中。EvaluationContextExpressionEvaluatingTransformerSpring中文文档

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

@Bean
public SpelFunctionFactoryBean xpath() {
    return new SpelFunctionFactoryBean(XPathUtils.class, "evaluate");
}
在父上下文中声明的 SpEL 函数也可在任何子上下文中使用。 每个上下文都有自己的工厂 Bean 实例,因为每个实例都需要不同的 ,但函数声明是继承的,可以通过声明具有相同名称的 SpEL 函数来覆盖。integrationEvaluationContextBeanResolver

内置 SpEL 函数

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

  • #jsonPath:计算指定对象上的“jsonPath”。 此函数调用 ,该函数委托给 Jayway JsonPath 库。 以下列表显示了一些使用示例:JsonPathUtils.evaluate(…​)Spring中文文档

    <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 方法(例如)提供。Spring中文文档

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

    有关 JSON 的更多信息,请参阅 Transformer 中的“JSON Transformers”。Spring中文文档

  • #xpath:在某个提供的对象上计算 xpath。 有关 XML 和 XPath 的更多信息,请参见 XML 支持 - 处理 XML 有效负载Spring中文文档

在父上下文中声明的 SpEL 函数也可在任何子上下文中使用。 每个上下文都有自己的工厂 Bean 实例,因为每个实例都需要不同的 ,但函数声明是继承的,可以通过声明具有相同名称的 SpEL 函数来覆盖。integrationEvaluationContextBeanResolver
使用此函数需要 Jayway JsonPath 库 () 位于类路径上。 否则,不会注册 SpEL 函数。json-path.jar#jsonPath

属性访问器

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

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

在前面的示例中,将两个自定义实例注入到 (按声明顺序) 。PropertyAccessorEvaluationContextSpring中文文档

要使用 Java 配置提供实例,您应该声明名称为 (由常量决定)的 Bean。 以下示例说明如何使用 Java 配置两个自定义实例:PropertyAccessorSpelPropertyAccessorRegistrarspelPropertyAccessorRegistrarIntegrationContextUtils.SPEL_PROPERTY_ACCESSOR_REGISTRAR_BEAN_NAMEPropertyAccessorSpring中文文档

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

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

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