1.在启动类上加上@EnableScheduling注解,表示启用定时任务
@SpringBootApplication()
@EnableScheduling
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
2.在需定时的方法上,加上注解@Scheduled
@Scheduled(cron = "0/5 * * * * *")
public void statisticsCategory() {
System.out.println(new Date());
}
3.直接运行就大功告成了,至于cron如何编写,网上有直接在线生成,也有在线解释的,可以自行百度。

本文介绍了如何在SpringBoot中设置定时任务,包括在启动类上启用@EnableScheduling注解,以及在具体方法上使用@Scheduled注解进行定时任务的定义。通过简单的步骤,即可实现定时任务的配置,并建议对cron表达式的编写可以通过在线工具辅助完成。
1451

被折叠的 条评论
为什么被折叠?



