将 Spring Integration 标头值设置为 bean 属性 [英] Set Spring Integration header value to bean property
问题描述
无论如何将 spring 集成头的值设置为 bean 属性.
Is there anyway to set value of spring integration header to bean property.
<int:header-enricher>
<int:header name="bId" expression="T(java.util.UUID).randomUUID()" />
</int:header-enricher>
现在在 bean 定义中
Now in bean definition
<bean id="" class="">
<property name="bId" value="#{headers['bId']}" />
</bean>
以上代码不起作用.这引发异常
This above code doesn't work. this throws exception
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'headers' cannot be found on object of type 'org.spri
ngframework.beans.factory.config.BeanExpressionContext' - maybe not public?
我尝试了以下方法,它们不起作用
I tried below ways, they don't work
<bean id="" class="">
<property name="bId" value="headers['bId']" />
</bean>
<bean id="" class="">
<property name="bId" ref="headers['bId']" />
</bean>
下面本来是理想的,但是这个表达式
不可用
Below would've been ideal, but this expression
is not available
<bean id="" class="">
<property name="bId" expression="headers['bId']" />
</bean>
推荐答案
Spring Integration 表达式如
Spring Integration expressions such as
<int:header name="bId" expression="T(java.util.UUID).randomUUID()" />
是运行时表达式 - 它们适用于流经系统的消息;在大多数情况下,表达式计算的根对象是 Message
.
are runtime expressions - they apply to messages flowing through the system; in most cases the root object for the expression evaluation is the Message
.
表达式如
<property name="bId" value="#{...}" />
是初始化时间 SpEL 表达式 - 它们在上下文初始化期间进行评估.还没有 Message
对象 - 评估的根对象是应用程序上下文,因此您可以执行诸如引用其他 bean 上的属性之类的操作
are initialization time SpEL expressions - they are evaluated during context initialization. There is no Message
object yet - the root object for the evaluation is the application context, so you can do things like referencing properties on other beans
<property name="bId" value="#{somebean.foo}" />
这些类型的表达之间存在很大差异.
There's a big difference between these types of expressions.
这篇关于将 Spring Integration 标头值设置为 bean 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!