Search code examples
springspring-el

Spring SpEL Wiring Collection


I was reading Spring in action these days and came across the following code. I'm wondering if the following code can actually work.

<property name="chosenCity" value="#{cities[T(java.lang.Math).random() * cities.size()]}"/>

It seems like

T(java.lang.Math).random() * cities.size()

will return a double value. How can we reference a double value index in a array? The author didn't explain this? I was wondering if Spring SpEL has any auto-converting mechanism to trim a double value to int? Thanks!


Solution

  • Yes, SpEL will provide necessary conversions (if it can). See section 7.3.1 in the Spring Reference Manual.