11
votes

I've got a (String, obviously) property expressed in minutes that I want to convert to an int before I do some arithmetic and inject it into my Spring bean. Right now I have this SpEL expression:

#{T(java.lang.Integer).parseInt(myProperties['MIN_TIME']) * 60 * 1000}

where myProperties is a simple java.util.Properties bean.

Not that I'm particularly annoyed by this expression, but nevertheless: does the SpEL have a prettier, built-in way to parse strings into numeric values?

Thanks!

2

2 Answers

14
votes

Doesn't look like it, e.g. look at how the developers create them here: https://jira.springsource.org/browse/SPR-8716

A slightly shorter version could be

#{new Integer(myProperties['MIN_TIME']) * 60 * 1000}
0
votes

Another way to do this is to use the util namespace to define a properties bean, ... xmlns:util="http://www.springframework.org/schema/util" ... http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd ...

<util:properties id="apiProperties"
    location="classpath:/api.properties" />

then use the alternate syntax for the properties bean

@Value("#{apiProperties['api.orders.pingFrequency']}")
private Integer pingFrequency;

The spring util properties bean generated by context will do the parse and conversion before assigning the value.