简体   繁体   中英

How to do arithmetic in Spring properties, with values from a properties file?

Example use-case: properties file passes in a numeric value in millis, and you want to use it in seconds.

Your .properties file:

jdbc.timeout= 2000

Your applicationContext.xml currently

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">       
   <property name="driverClassName" value="com.mysql.jdbc.Driver" />        
   <property name="url"value="jdbc:mysql://myserver/mydb" />
   <property name="username" value="user" />        
   <property name="password" value="changeme" />        
   <property name="validationQuery" value="SELECT 1;"/>         
   <property name="validationQueryTimeout" value="${jdbc.timeout}" />
</bean>

You want the validationQueryTimeout value to be in seconds, how do you convert it?

如果您使用的是Spring 3.0或更高版本,则可以使用SpEL表达式内的属性,如下所示:

   <property name="validationQueryTimeout" value="#{${jdbc.timeout}/1000}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM