[英]Spring @Scheduled inject delay time
I have a few methods annotated with 我有一些注释的方法
@Scheduled(fixedDelay = 6000)
private void myScheduledMethod(){
//do something
}
I also have a set of properties files where I configure environment specific values. 我还有一组属性文件,我在其中配置特定于环境的值。 For testing purposes I'd like the value of the delay to be configurable, ideally through a property in a properties file.
出于测试目的,我希望可以配置延迟的值,理想情况是通过属性文件中的属性。
Since the value of fixedDelay
has to be a constant, I'm looking for a way to get this set from a properties file, but haven't found a way to do it yet. 由于
fixedDelay
的值必须是常量,我正在寻找一种从属性文件中获取此集的方法,但还没有找到方法。
I got stuck on the same issues but the best way to solve this now would be: 我遇到了同样的问题,但现在解决这个问题的最佳方法是:
@Scheduled(fixedDelayString = "${my.delay.property}")
public void myScheduledMethod(){
// do something
}
It would be good to have this option, but I think it does not exist (the annotation is class-level, while the value would be injected when an instance is created). 拥有此选项会很好,但我认为它不存在(注释是类级别,而值将在创建实例时注入)。
In order to make this configurable use the xml namespace <task:
. 为了使这个可配置使用xml命名空间
<task:
. Like the example from the spring docs : 像春季文档中的示例:
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="someObject" method="someMethod"
fixed-delay="${configuredDelay}"/>
</task:scheduled-tasks>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.