如何使用 Spring 3.0 表达式语言参数化 @Scheduled(fixedDelay)? [英] How to parameterize @Scheduled(fixedDelay) with Spring 3.0 expression language?
问题描述
当使用 Spring 3.0 功能注释计划任务时,我想将 fixedDelay
设置为我的配置文件中的参数,而不是像当前一样将其硬连接到我的任务类中...
When using the Spring 3.0 capability to annotate a scheduled task, I would like to set the fixedDelay
as parameter from my configuration file, instead of hard-wiring it into my task class, like currently...
@Scheduled(fixedDelay = 5000)
public void readLog() {
...
}
不幸的是,似乎使用 Spring 表达式语言 (SpEL) @Value
返回一个 String 对象,而该对象又无法按照fixedDelay
参数.
Unfortunately it seems that with the means of the Spring Expression Language (SpEL) @Value
returns a String object which in turn is not able to be auto-boxed to a long value as required by the fixedDelay
parameter.
推荐答案
我猜 @Scheduled
注释是不可能的.因此,您的解决方案可能是使用 task-scheduled
XML 配置.让我们考虑这个例子(复制自 Spring doc):
I guess the @Scheduled
annotation is out of question. So maybe a solution for you would be to use task-scheduled
XML configuration. Let's consider this example (copied from Spring doc):
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="someObject" method="readLog"
fixed-rate="#{YourConfigurationBean.stringValue}"/>
</task:scheduled-tasks>
... 或者如果从 String 到 Long 的强制转换不起作用,则如下所示:
... or if the cast from String to Long didn't work, something like this would:
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="someObject" method="readLog"
fixed-rate="#{T(java.lang.Long).valueOf(YourConfigurationBean.stringValue)}"/>
</task:scheduled-tasks>
同样,我还没有尝试过这些设置中的任何一个,但我希望它可以对您有所帮助.
Again, I haven't tried any of these setups, but I hope it might help you a bit.
这篇关于如何使用 Spring 3.0 表达式语言参数化 @Scheduled(fixedDelay)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!