1.在启动类上添上@EnableScheduling
@EnableScheduling
@EnableTransactionManagement
@EnableDiscoveryClient
@EnableFeignClients //服务调用
@ComponentScan("com.atguigu")
@SpringBootApplication//取消数据源自动配置
@MapperScan("com.atguigu.staservice.mapper")
public class StaApplication {
public static void main(String[] args) {
SpringApplication.run(StaApplication.class, args);
}
}
2.写一个ScheduleTask类在需要定时执行的方法上加上 @Scheduled
@Component
public class ScheduleTask {
@Autowired
private StatisticsDailyService statisticsDailyService;
//在每天的凌晨一点,把前一天的数据进行添加
@Scheduled(cron = "0 0 1 * * ?")
public void task1() {
//DateUtil工具类生成前一天时间
String day = DateUtil.formatDate(DateUtil.addDays(new Date(), -1));
statisticsDailyService.countRegister(day);
}
//每隔5秒执行一次
// @Scheduled(cron = "0/5 * * * * ?")
// public void task1() {
// long l = System.currentTimeMillis();
// System.out.println("*********++++++++++++*****执行了"+l);
// }
}
3.在线Cron表达式生成器



