我有这样的代码:
private static final String FOURTEEN_MIN = "PT14M";
...
@Scheduled(cron = "0 */15 * * * *")
@SchedulerLock(name = "scheduledTaskName", lockAtMostForString = FOURTEEN_MIN, lockAtLeastForString = FOURTEEN_MIN)
public void scheduledTask() {
// do something
}
现在我使用 lockAtMostForString
常量,但我想从属性中获取此值。
有办法做到吗?
附注
我知道我不能使用注释并像这样重写它:
LockingTaskExecutor executor = new DefaultLockingTaskExecutor(lockProvider);
...
Instant lockAtMostUntil = Instant.now().plusSeconds(600);
executor.executeWithLock(runnable, new LockConfiguration("lockName", lockAtMostUntil));
但我更喜欢使用注释。
最佳答案
您可以使用 spring ${propertyName:defaultValue}
中的注入(inject)语法将属性直接注入(inject)到注释中。
注1:当没有声明defaultValue
时,如果缺少该属性,spring将抛出错误。
注解 2:此语法几乎可用于所有 spring 注释。
关于java - Shedlock:使用属性文件中的 lockAtMostForString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54786372/