Sebastian
Sebastian

Reputation: 1835

Evaluating properties inside Spring Expression Lang (SpEL)

Our service has a process that is scheduled according to a properties file, reading the property refreshIntervalMillis. Its value is injected directly in a Quartz trigger with this configuration:

<bean name="trigger"
    class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean "
    p:repeatInterval="${refreshIntervalMillis}"> 
...
</bean>

However, the admins that install this service think in terms of hours/days, so in order to make thing easier for them, we changed this to:

  1. Renamed refreshIntervalMillis to refreshIntervalMinutes
  2. Changed to code above to the following:
p:repeatInterval="#{ 1000 * 60 * T(java.lang.Integer).valueOf(@configurationProperties['garbageLevelWatcher.refreshIntervalMinutes'])}"

Note: the properties object is exposed as a bean named "configurationProperties"

Is there a simpler syntax to accomplish the same?

Thanks,

Upvotes: 6

Views: 13037

Answers (2)

Gary Russell
Gary Russell

Reputation: 174554

"#{T(java.util.concurrent.TimeUnit).MINUTES.toMillis( @configurationProperties['garbageLevelWatcher.refreshIntervalMinutes'])}"

EDIT:

Or...

<context:property-placeholder properties-ref="configurationProperties"
<util:constant id = "MINUTES" static-field="java.util.concurrent.TimeUnit.MINUTES" />

and

"#{@MINUTES.toMillis(${garbageLevelWatcher.refreshIntervalMinutes})}"

Upvotes: 7

emd
emd

Reputation: 750

If the properties are looked up by a PropertyPlaceholderConfigurer, @PropertySource or <context:property-placeholder /> and the context is aware of it

You can write it like this:

p:repeatInterval="#{ 1000 * 60 * T(java.lang.Integer).valueOf('${garbageLevelWatcher.refreshIntervalMinutes}') }"

Upvotes: 1

Related Questions