首先,依据SpringBoot官方文档。
点击进入之后,就是SpringBoot给出的参考指南,简单看一遍怎么样的流程之后,就下载定时任务源代码
然后,分析源代码(就两步)。
第一、在项目启动项前加@EnableScheduling注解
@EnableScheduling
ensures that a background task executor is created. Without it, nothing gets scheduled.
第二、在需要执行定时任务的类ScheduledTasks 前加@Component,以便Spring能扫描到这个类。再在业务方法ScheduledTasks 前加@Scheduled(fixedRate = 5000)注解即可。
The Scheduled
annotation defines when a particular method runs. NOTE: This example uses fixedRate
, which specifies the interval between method invocations measured from the start time of each invocation.
You can also use@Scheduled(cron=". . .")
expressions for more sophisticated task scheduling