Set Spring Integration header value to bean proper

2019-05-28 23:54发布

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条回答
够拽才男人
2楼-- · 2019-05-29 00:16

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.

查看更多
登录 后发表回答