我有这样的代码:
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));
但是我更喜欢使用注释。
答案 0 :(得分:1)
您可以使用spring ${propertyName:defaultValue}
中的注入语法将属性直接注入注释中。
注1:当不声明defaultValue
时,如果缺少该属性,spring将抛出错误。
注2:几乎每个spring注释都可以使用此语法。