Set Spring Integration header value to bean property

你离开我真会死。 提交于 2019-12-07 19:41:23

问题


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>

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>

回答1:


Spring Integration expressions such as

<int:header name="bId" expression="T(java.util.UUID).randomUUID()" />

are runtime expressions - they apply to messages flowing through the system; in most cases the root object for the expression evaluation is the Message.

Expressions such as

<property name="bId" value="#{...}" />

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.



来源:https://stackoverflow.com/questions/39383590/set-spring-integration-header-value-to-bean-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!