1.SpringBoot使?用注解?方式开启定时任务
@SpringBootApplication @ServletComponentScan //启动类?里里?面 @EnableScheduling开启定时任务,?自动扫描 @EnableScheduling public class Demo001Application { public static void main(String[] args) { SpringApplication.run(Demo001Application.class, args); } }
package com.xiaobing.demo001.schedule; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.time.LocalDateTime; //定时任务业务类 加注解 @Component被容器?扫描 @Component public class TimingTask { //定时任务,5秒执行一次 //@Scheduled(fixedRate = 5000) //cron 定时任务表达式 @Scheduled(cron="*/1 * * * * *") 表示每秒 @Scheduled(cron="*/5 * * * * *") public void sum1() { //业务逻辑省略 System.out.println("定时任务: " + LocalDateTime.now()); } }
启动项目:
crontab 在线工具参考:https://tool.lu/crontab/
原文:https://www.cnblogs.com/xiaozhaoboke/p/14653771.html