2022-9-19 小问题 大错误

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.............");
//    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值