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。

相关推荐
m0_587383005 分钟前
广州 24 小时自助健身房解决方案技术实现与实战指南
java·spring·小程序·架构·需求分析
·云扬·20 分钟前
Prompt 工程与安全:从 System 设计到注入防御
java·人工智能·安全·ai·prompt
代码不停1 小时前
自动化测试(java)
java·开发语言·selenium·自动化
唠点键盘之外的2 小时前
26 向量数据库实战:从 0 搭一条能用的检索链路(pgvector + Spring AI)
数据库·人工智能·spring
鬼手点金3 小时前
opencode-rules 插件
java·开发语言·前端·人工智能·chrome·git·github
paopaokaka_luck4 小时前
滑雪场管理系统(AI智能助手流式对话,ECharts 运营数据分析、教练、雪具与场地一体化预约,钱包支付退款、订单核销与服务评价)
java·人工智能·spring boot·网络协议·数据分析·echarts
SL_staff4 小时前
JVS混合架构实践:能力引擎+动态模型如何支撑企业级可持续交付
spring boot·低代码·全栈
迪飞特科技4 小时前
智慧物业平台实战:Spring Boot 整合小程序实现报修巡检费用全流程
spring boot·后端·小程序
考虑考虑4 小时前
@JsonInclude注解使用
spring boot·后端·spring
Wang's Blog4 小时前
Java 项目实战: 交友项目 - Dubbo 的内部结构与一次调用的完整链路
java·dubbo