SpringBoot 执行定时任务---cron 表达式

  1. 主启动类上开启定时任务,使用 @EnableScheduling
  2. 在需要定时执行的方法上,使用 @Scheduled(cron = " ")
  3. cron 表达式:设置执行的规则
    3.1. 可借助在线工具生成需要的 cron 表达式,例如https://cron.qqe2.com/
@Component
public class ScheduleTask {
    
    

    // 0/5 * * * * ?表示每隔5秒执行一次这个方法
    @Scheduled(cron = "0/5 * * * * ?")
    public void task() {
    
    
        System.out.println("定时任务执行了");
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_51681634/article/details/111320314
今日推荐