一个方法有多个@Scheduled注解

Multiple @Scheduled annotations at one method

是否允许/可以在一种方法中使用多个@Scheduled 注释?

@Scheduled(cron = "0 5 0 * * *", zone = "Europe/Stockholm")
@Scheduled(fixedRate = 1000 * 60 * 20, initialDelay = 1000 * 60 * 5)
public void setSalariesAsArchived() {
//...
}

是的,这是完全合法的,因为 @Scheduled@Repeatable 注释,如 the Javadoc for @Schedules

中所述

Container annotation that aggregates several Scheduled annotations. Can be used natively, declaring several nested Scheduled annotations. Can also be used in conjunction with Java 8's support for repeatable annotations, where Scheduled can simply be declared several times on the same method, implicitly generating this container annotation.

所以你既可以像以前一样使用它,也可以像下面的例子那样使用@Schedules包装它

@Schedules({
    @Scheduled(cron = "0 5 0 * * *", zone = "Europe/Stockholm"),
    @Scheduled(fixedRate = 1000 * 60 * 20, initialDelay = 1000 * 60 * 5)
})
public void setSalariesAsArchived() {
//...
}