简体   繁体   中英

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:

//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:

<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. But the first two solutions are more efficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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