本文已使用 Google Cloud Translation API 自动翻译。
某些文档最好以原文阅读。
在这篇文章中,我们将讨论如何在 Spring Boot 应用程序中实现计划任务。 Spring Boot 提供了一种简单方便的方式来运行计划任务。
Spring Boot 提供了一个 @Scheduled 注释,可以在方法上使用它来将它们标记为应该定期运行的任务。例如,我们可以使用@Scheduled 注解每分钟运行一个任务:
@Scheduled(cron = "0 * * * * ?")
public void runEveryMinute() {
// do something
}
在上面的示例中,我们使用 cron 表达式来安排任务。 Cron 表达式是指定何时运行任务的便捷方式。有关 cron 表达式的更多信息,请参阅 CronTrigger javadoc。
要在我们的 Spring Boot 应用程序中运行任务,我们只需要使用 @EnableScheduling 注释来注释我们的主类:
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}