Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary operator in Spring

My question is similar to this question. Since that question is quite old so thought of posting new question.

I am also writing my expression in following

<property name="to" value="#{ systemProperties['BR']} == '01' ? 
    ${PROPERTY_VALUE_1_FROM_BUNDLE} : 
    ${PROPERTY_VALUE_2_FROM_BUNDLE}" />

When I fetch the value of "to" variable from my bean. Its giving me something like below

01='01'? value1 : value2

Its not parsing my expression in XML itself.

Am I doing anything wrong here?

like image 630
Jaikrat Avatar asked Jul 12 '26 09:07

Jaikrat


1 Answers

You are terminating the SpEL too early; it should be...

<property name="to" value="#{ systemProperties['BR'] == '01' ? 
    '${PROPERTY_VALUE_1_FROM_BUNDLE}' : 
    '${PROPERTY_VALUE_2_FROM_BUNDLE}' }" />

Note that you also need single quotes around the placeholders so the resolved values are treated as literals.

like image 56
Gary Russell Avatar answered Jul 15 '26 12:07

Gary Russell