Affects: Spring Framework\5.2.5, Spring Boot\2.2.6
If the SPEL ternary operator is used in @Value, application.properties or application.yml its not evaluated correctly. Not matter what the expression result is it always returns the "false value".
The following application
@SpringBootApplication
@ConfigurationProperties(prefix = "application")
class SpelTernaryApplication {
var spel = ""
var spel2 = ""
@Value("\${true ? 'trueExp' : 'falseExp'}")
lateinit var spel3: String
}
application.spel=${true ? trueExp : falseExp}
application.spel2=${2 > 1 ? a : b}
@SpringBootTest
class SpelTernaryApplicationTests {
@Autowired
lateinit var bean: SpelTernaryApplication
@Test
fun spelEvaluation() {
val parser: ExpressionParser = SpelExpressionParser()
val parserResult = parser.parseExpression("true ? 'trueExp' : 'falseExp'").getValue(String::class.java)
println("parser: $parserResult")
println("property: ${bean.spel}")
println("property: ${bean.spel2}")
println("@Value property: ${bean.spel3}")
assert("trueExp" == parserResult) { "parser: $parserResult" }
assert("trueExp" == bean.spel) { "property: ${bean.spel}" }
assert("a" == bean.spel2) { "property: ${bean.spel2}" }
assert("trueExp" == bean.spel3) { "@Value property: ${bean.spel3}" }
}
}
produces
parser: trueExp property: falseExp property: b @Value property: 'falseExp'
property: falseExp java.lang.AssertionError: property: falseExp at com.example.spelternary.SpelTernaryApplicationTests.spelEvaluation(SpelTernaryApplicationTests.kt:26) [...]
example project: https://github.com/jackhammer2k/spring-spel-ternary-issue
Comment From: sbrannen
That's not a SpEL expression.
That's a property placeholder (${..}) with a "default value".
For a SpEL expression, you need to use the #{...} syntax. For further details, please consult the reference manual.