user244333
user244333

Reputation:

How to do arithmetic in Spring properties?

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail" ref="Job1" />
        <property name="repeatInterval" value="1" />
    </bean>

I want to load value from a property file (value = "${jobs.per.second}"), which will be manipulated.

For example: jobs.properties file will have: jobs.per.second = 500

I want to use arithmetic operations (invert it and multiply it by 1000) (1/500 * 1000 = 2) and substitute value = 2 instead of 1.

How do I go about it? Is there any way to enable arithmetic operations in xml?

EDIT: I am using Spring 3.

Upvotes: 12

Views: 8992

Answers (1)

Andrew Newdigate
Andrew Newdigate

Reputation: 6205

You didn't mention which version of Spring you're using, but Spring 3.0 comes with Spring EL (Expression Language) which allows you to use expressions in the XML bean definitions (as well as other places, such as @Value annotations).

<util:properties id="properties" location="classpath:jobs.properties"/>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
    <property name="jobDetail" ref="Job1" />
    <property name="repeatInterval" value="#{ 1000 / properties['jobs.per.second'] * 100.0 }" />
</bean>

You can read more about Spring EL here

Upvotes: 12

Related Questions