如何在Spring属性中进行算术运算? [英] 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>
我想从物业加载值
文件( value =$ {jobs.per.second}
),将被操纵。
I want to load value
from a property file (value = "${jobs.per.second}
"), which will be manipulated.
For示例: jobs.properties
文件将包含: jobs.per.second = 500
For example: jobs.properties
file will have: jobs.per.second = 500
我想使用算术运算(将其反转并乘以1000)(1/500 * 1000 = 2)
并替换 value = 2
而不是1.
I want to use arithmetic operations (invert it and multiply it by 1000) (1/500 * 1000 = 2)
and substitute value = 2
instead of 1.
我该怎么办?有没有办法在xml中启用算术运算?
How do I go about it? Is there any way to enable arithmetic operations in xml?
编辑:我使用的是Spring 3.
I am using Spring 3.
推荐答案
你没有提到你正在使用的是哪个版本的Spring,但Spring 3.0附带了Spring EL(表达式语言),它允许你在XML bean定义中使用表达式(以及其他地方,例如@Value注释)。
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>
您可以阅读有关Spring EL的更多信息在其他地方
You can read more about Spring EL here
这篇关于如何在Spring属性中进行算术运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!