在父摘要中使用@Scheduler class

Use @Scheduler in a parent abstract class

我创建了这样一个摘要 class:

abstract class ScheduledProcess {

  abstract List<String> fetchNewContent()
  abstract List<String> cron()

  //This SPeL doesn't work just stating what kind of expression I'm looking for
  @Scheduled(cron='#{this.cron()}')
  void persistContent(){
     doSomeStuffWithContent(fetchNewContent())
  }

}

我的目标是不要重复自己必须在所有子class 中实施@Scheduled 方法。 cron() 方法 returns 具体的 subclass cron 表达式。但是我没有找到将 cron 值传递给注释的方法。可能是我看问题的方式不对。

你不能在那里使用 SpEL,只能使用 属性 占位符 ${...}

我认为这现在是可能的(Spring 4.3.0)你可以在问题中看到它。

https://jira.spring.io/browse/SPR-13625

如果您使用的是另一个 Spring 版本,您可以编写自己的 bean 后处理器。您可以在 quantum here

给出的答案中查看示例

Injecting externalized value into Spring annotation