1.SpringBoot定时任务@EnableScheduling
1.1 定时任务作用?
定时任务相当于闹钟
在什么时间做什么事情(执行什么命令/脚本)
2.2写一个springboot的启动类:
启动类里面使用@EnableScheduling 注解开启功能,自动扫描
@SpringBootApplication
@EnableScheduling //开启定时任务
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
2.3 要在任务的类上写@Component 要在任务方法上写@Scheduled
@Component
public class ScheduledTask {
@Autowired
private StatisticsDailyService statisticsDailyService;
// 每天凌晨一点执行程序把数据查询进行添加 查询并添加的是前一天的数据
@Scheduled(cron = "0 0 1 * * ?")
public void task2() {
statisticsDailyService.registerCount(DateUtil.formatDate(DateUtil.addDays(new Date(), -1)));
}
@Scheduled(cron = "0 0 12 * * ?")
public void task1() {
statisticsDailyService.registerCount(DateUtil.formatDate(DateUtil.addDays(new Date(), -1)));
}
//
// @Scheduled(cron = "0 0/2 * * * ?")
// public void task3() {
// statisticsDailyService.registerCount(DateUtil.formatDate(DateUtil.addDays(new Date(), -1)));
// System.out.println("task3.............");
// }
}