简体   繁体   English

在 Spring 集成中,如何在路由器通道中使用 Util 常量

[英]In Spring integration, how do I use Util constant in router channel

I've a use case where I've defined all my constants in one class.我有一个用例,我在一个类中定义了所有常量。

public class MyExampleConstants {
    public static String FOO1 = "hulcSmash";
    public static String FOO2 = "billionairePlayBoy";
    public static String FOO3 = "capShield";
}

Right now I'm using something like this in routerChan for mapping values:现在我在 routerChan 中使用类似的东西来映射值:

//using exact string match in mapping value, however, want to see if the constants be used here.
<int:router input-channel="powRouterChan" resolution-required="false" expression="payload.avengersVO.powType" default-output-channel="flowEndPoint">
    <int:mapping value="hulcSmash" channel="avengersHulcChan" />
    <int:mapping value="billionairePlayBoy" channel="avengersIRManChan" />
    <int:mapping value="capShield" channel="avengersCapAmericaChan" />
</int:router>

and In router expresstions something like this:在路由器表达式中是这样的:

//Now these placeholders are being configured in properties
<int:router input-channel="channelABC" expression=" !payload.avengersVO.powType.equals('${avengers.hulc.smash}') 
    and !payload.avengersVO.powType.equals('${avengers.billionaire.PlayBoy}') 
    and !payload.avengersVO.powType.equals('${avengers.capShield}') ? 'flowEndpoint' : 'civilWarsChan'"/>

Now I've added my constants into my context and want to use them in my router channel as mapping values:现在我已将常量添加到我的上下文中,并希望在我的路由器通道中将它们用作映射值:

<util:constant id="foo1" static-field="com.marvel.avengers.hulc.util.MyExampleConstants.FOO1"/>
<util:constant id="foo2" static-field="com.marvel.avengers.hulc.util.MyExampleConstants.FOO2"/>
<util:constant id="foo3" static-field="com.marvel.avengers.hulc.util.MyExampleConstants.FOO3"/>

<int:router input-channel="powRouterChan" resolution-required="false" expression="payload.avengersVO.powType" default-output-channel="flowEndPoint">
    <int:mapping value="foo1" channel="avengersHulcChan" />
    <int:mapping value="foo2" channel="avengersIRManChan" />
    <int:mapping value="foo3" channel="avengersCapAmericaChan" />
</int:router>

and want to update the expression above to something like this, using constants that I've declared above:并希望使用我在上面声明的常量将上面的表达式更新为类似的内容:

    <int:router input-channel="channelABC" expression=" !payload.avengersVO.powType.equals('foo1') 
    and !payload.avengersVO.powType.equals('foo2') 
    and !payload.avengersVO.powType.equals('foo3') ? 'flowEndpoint' : 'civilWarsChan'"/>

Please suggust how can I use this.请建议我如何使用它。 Thanks in advance.提前致谢。

You can do it directly in the original mapping router, using the T operator:您可以使用T运算符直接在原始映射路由器中执行此操作:

<int:router input-channel="powRouterChan" resolution-required="false" expression="payload.avengersVO.powType" default-output-channel="flowEndPoint">
    <int:mapping value="#{T(com.marvel.avengers.hulc.util.MyExampleConstants).FOO1}" channel="avengersHulcChan" />
    ...
</int:router>

or using the static:或使用静态:

<int:router input-channel="powRouterChan" resolution-required="false" expression="payload.avengersVO.powType" default-output-channel="flowEndPoint">
    <int:mapping value="#{foo1}" channel="avengersHulcChan" />
    ...
</int:router>

If you must do it in the expression for some reason, use @foo1 where the @ means reference to a bean.如果出于某种原因必须在表达式中执行此操作,请使用@foo1 ,其中@表示对 bean 的引用。 But the first two solutions are more efficient.但前两种解决方案更有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM