java 评估 Spring Expression Lang (SpEL) 中的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15510563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:51:21  来源:igfitidea点击:

Evaluating properties inside Spring Expression Lang (SpEL)

javaspringpropertiesexpressionspring-el

提问by Sebastian

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:

我们的服务有一个根据属性文件调度的进程,读取属性refreshIntervalMillis。它的值通过以下配置直接注入 Quartz 触发器中:

<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 refreshIntervalMillisto refreshIntervalMinutes
  2. Changed to code above to the following:
  1. 更名refreshIntervalMillisrefreshIntervalMinutes
  2. 将上面的代码改为如下:
p:repeatInterval="#{ 1000 * 60 * T(java.lang.Integer).valueOf(@configurationProperties['garbageLevelWatcher.refreshIntervalMinutes'])}"

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

注意:属性对象公开为名为“configurationProperties”的 bean

Is there a simpler syntax to accomplish the same?

是否有更简单的语法来完成相同的操作?

Thanks,

谢谢,

回答by Gary Russell

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

"#{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})}"

回答by emd

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

如果通过 PropertyPlaceholderConfigurer、@PropertySource 或 <context:property-placeholder /> 查找属性并且上下文知道它

You can write it like this:

你可以这样写:

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