SpringBoot自带的定时任务

1.在主类上添加

复制代码
@EnableScheduling 注解
java 复制代码
@EnableScheduling

@ComponentScan("com.atguigu")
@SpringBootApplication
@MapperScan("com.atguigu.staservice.mapper")
public class StaApplication {
    public static void main(String[] args) {
        SpringApplication.run(StaApplication.class, args);
    }
}

2.创建任务模块,即可完成定时任务

java 复制代码
@Component
public class ScheduleTask {
    @Autowired
    StatisticsDailyService statisticsDailyService;

    /**
     * 定时任务,每天凌晨12点更新前1天 的统计信息 (包括注册人数、登录人数等)
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void task2() {
        statisticsDailyService.registerCount(DateUtil.formatDate(DateUtil.addDays(new Date(), -1)));
    }
}

和整合quartz的区别是 1.不能持久化(不能存到数据库中) 2.不能满足 需要高度定制和复杂调度的场景,比如需要定时任务1执行完, 立即执行定时任务2。

相关推荐
码少女1 小时前
MySQL--库与表的增删改查基础
java·数据库·mysql
Flynt3 小时前
掘金热文说 Spring Boot 4.1 默认开虚拟线程,我实测后发现了更狠的坑
java·spring boot·性能优化
拄杖忙学轻声码3 小时前
Spring中的 HttpHeaders.readOnlyHttpHeaders() 作用是什么
spring
Resean02233 小时前
Spring 源码分析(一)IOC 容器总览与 refresh() 全景
java·后端·spring
保利威视频SaaS小张4 小时前
企业直播接入笔记 12|企业内训直播怎么做签到和学时统计,防挂机这件事比想象中难
java·前端·数据库·笔记·视频云·polvy·polyv
开开心心就好5 小时前
两张表格数据对比工具,不用写函数也能查
算法·spring·智能手机·pdf·电脑·intellij-idea·音视频
顧棟5 小时前
【ES实战】 索引模板「可追溯、可校验」:一种模板治理的工程化思路
java·数据库·elasticsearch
玄昌盛不会编程6 小时前
LeetCode——2243. 计算字符串的数字和
java·算法·leetcode
七夜zippoe6 小时前
流式输出:让 AI 回答像打字机一样实时返回(SSE 全链路实战)
人工智能·spring·ai·流式输出·ssf
文丑颜不良啊7 小时前
LeetCode3061~3080题目解析
java·leetcode