【问题标题】:Use @Scheduler in a parent abstract class在父抽象类中使用 @Scheduler
【发布时间】:2016-07-13 14:35:11
【问题描述】:

我创建了一个这样的抽象类:

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())
  }

}

我的目标是不要重复自己必须在所有子类中实现 @Scheduled 方法。 cron() 方法返回特定的子类 cron 表达式。但是我没有找到将 cron 值传递给注释的方法。也许我只是以错误的方式看待这个问题。

【问题讨论】:

  • 请添加您遇到的异常/堆栈跟踪/错误

标签: java spring groovy spring-boot spring-el


【解决方案1】:

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

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

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

给出的答案中看到一个示例

Injecting externalized value into Spring annotation

【讨论】:

  • @Scheduled(cron='#{this.cron()}') spring boot 支持吗?我也有类似的问题
【解决方案2】:

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

【讨论】:

  • 您能否详细说明为什么无法使用 SpEL? @Scheduled 注释是特定的限制吗?使用属性占位符您知道如何实现我想要的吗?
  • 只是 ScheduledAnnotationBeanPostProcessor 没有逻辑来评估 SpEL 表达式。需要添加这样的逻辑,将当前 bean 作为 #root 评估对象(或以其他方式作为 SpEL 变量提供,例如 #currentBean)。不,它不能使用占位符来完成。您可以编写自己的 bean 后处理器并使用它来代替标准的;但它需要对 Spring 内部和 SpEL 有很好的理解。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-02
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多