SpringTask二:注解配置方式
一.applicationContext.xml
pom.xml与第一篇一样
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--开启注解扫描--> <context:component-scan base-package="com.wj"/> <!--开启对@Scheduled注解的支持--> <task:annotation-driven/> </beans>
二.任务类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | @Service public class TaskService { //initialDelay:服务启动后,多少毫秒启动该定时任务 //fixedDelay:每隔多长时间执行一次定时任务 //cron:cron表达式,复杂任务 @Scheduled (initialDelay = 10000 ,fixedDelay = 1000 ) public void firstTask(){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd-HH-mm:ss:ms" ); String format = simpleDateFormat.format( new Date()); System.out.println( "数据库备份时间1:" +format); } @Scheduled (initialDelay = 20000 ,fixedDelay = 2000 ) public void secondTask(){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd-HH-mm:ss:ms" ); String format = simpleDateFormat.format( new Date()); System.out.println( "数据库备份时间2:" +format); } } |
三.执行结果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix