定时任务之@Scheduled注解
-
首先,在SpringBoot启动类上加@EnableScheduling 注解,开启定时服务
-
该注解直接添加到定时任务类上是否可生效待验证
-
创建定时任务类:
-
定时任务类上必须加注解 @Component 注解(当然也可以使用 @Controller 和 @Service 等其他与 @Component 作用相同的注解)
-
定时方法上加上@Scheduled 注解:
@Scheduled注解参数描述:
@Scheduled(fixedRate=3000):上一次开始执行时间点后3秒再次执行;
@Scheduled(fixedDelay=3000):上一次执行完毕时间点3秒再次执行;
@Scheduled(initialDelay=1000, fixedDelay=3000):第一次延迟1秒执行,然后在上一次执行完毕时间点3秒再次执行;
@Scheduled(cron="* * * * * ?"):按cron规则执行;
-