springBoot定时任务

springBoot定时任务,第1张

记录工作中使用定时的 *** 作,以备在用。

Spring Boot实现定时任务只需两步,开启定时任务、添加任务。

开启定时任务:

@SpringBootApplication
@EnableScheduling // 开启定时任务功能
public class DemoApplication {

}

添加任务:

@Component // 把此类托管给 Spring,不能省略
public class TaskUtils {
    // 添加定时任务
    @Scheduled(cron = "59 59 23 0 0 5") // cron 表达式,每周五 23:59:59 执行
    public void doTask(){
        System.out.println("我是定时任务~");
    }
}

参考:定时任务最简单的3种实现方法(超好用) - 知乎 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/725625.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存