Spring @Scheduled注入延迟时间

Ike*_*nez 7 java spring quartz-scheduler

我有一些注释的方法

@Scheduled(fixedDelay = 6000)
private void myScheduledMethod(){
//do something
}
Run Code Online (Sandbox Code Playgroud)

我还有一组属性文件,我在其中配置特定于环境的值.出于测试目的,我希望可以配置延迟的值,理想情况是通过属性文件中的属性.

由于值fixedDelay必须是常量,我正在寻找一种从属性文件中获取此集的方法,但还没有找到方法.

小智 6

我遇到了同样的问题,但现在解决这个问题的最佳方法是:

@Scheduled(fixedDelayString = "${my.delay.property}")
public void myScheduledMethod(){
    // do something
}
Run Code Online (Sandbox Code Playgroud)


Boz*_*zho 5

拥有此选项会很好,但我认为它不存在(注释是类级别,而值将在创建实例时注入).

为了使这个可配置使用xml命名空间<task:.像春季文档中的示例:

<task:scheduled-tasks scheduler="myScheduler">
    <task:scheduled ref="someObject" method="someMethod" 
         fixed-delay="${configuredDelay}"/>
</task:scheduled-tasks>
Run Code Online (Sandbox Code Playgroud)