Springboot如何通过Scheduled实现定时任务
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,今天小编给大家分享一下Springboot如何通过Scheduled实现定时任务的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇
千家信息网最后更新 2026年11月01日Springboot如何通过Scheduled实现定时任务
今天小编给大家分享一下Springboot如何通过Scheduled实现定时任务的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
定时任务一般会存在中大型企业级项目中,为了减少服务器、数据库的压力往往会采用时间段性的去完成某些业务逻辑。比较常见的就是金融服务系统推送回调,一般支付系统订单在没有收到成功的回调返回内容时会持续性的回调,这种回调一般都是定时任务来完成的。还有就是报表的生成,我们一般会在客户访问量过小的时候来完成这个操作,那往往都是在凌晨。这时我们也可以采用定时任务来完成逻辑。SpringBoot为我们内置了定时任务,我们只需要一个注解就可以开启定时为我们所用了。
在开发中,定时任务是常见的功能,在spring boot 下开发定时任务其实很简单,具体代码如下:
1、配置依赖包pom.xml
由于默认的maven仓库经常访问不了,这里采用了阿里云的maven仓库镜像。
4.0.0 com.example demo 0.0.1-SNAPSHOT jar spring-boot-scheduled Demo project for Spring Boot public aliyun nexus http://maven.aliyun.com/nexus/content/groups/public/ true public aliyun nexus http://maven.aliyun.com/nexus/content/groups/public/ true false org.springframework.boot spring-boot-starter-parent 1.4.5.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
2、定制任务场景
定时任务实现,提供固定周期、固定周期延迟间隔和制定时间点执行等场景。采用@Scheduled注解进行标注。
ExampleTimer.java
package com.example;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Componentpublic class ExampleTimer { SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 10000) public void timerRate() { System.out.println(dateFormat.format(new Date())); } //第一次延迟1秒执行,当执行完后2秒再执行 @Scheduled(initialDelay = 1000, fixedDelay = 2000) public void timerInit() { System.out.println("init : "+dateFormat.format(new Date())); } //每天20点16分50秒时执行 @Scheduled(cron = "50 16 20 * * ?") public void timerCron() { System.out.println("current time : "+ dateFormat.format(new Date())); }}3、启动应用程序
启动程序,需要增加@EnableScheduling注解.
SpringBootScheduledApplication.java
package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication@EnableSchedulingpublic class SpringBootScheduledApplication { public static void main(String[] args) { SpringApplication.run(SpringBootScheduledApplication.class, args); }}4、输出结果
20:16:27init : 20:16:28init : 20:16:30init : 20:16:32init : 20:16:34init : 20:16:3620:16:37init : 20:16:38init : 20:16:40init : 20:16:42init : 20:16:44init : 20:16:4620:16:47init : 20:16:48current time : 20:16:50init : 20:16:50init : 20:16:52init : 20:16:54
以上就是"Springboot如何通过Scheduled实现定时任务"这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注行业资讯频道。
任务
知识
篇文章
内容
就是
注解
逻辑
仓库
周期
场景
常见
时间
程序
系统
UTF-8
延迟
开发
服务
不同
很大
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
瑰夏咖啡
智能科技公司是互联网公司吗
合肥互联网科技公司
青岛互联网科技公司
为想互联网科技
奇酷互联网科技公司
互联网 视角下的科技创新
互联网科技宣传片
互联网金融科技委员会
恒生科技互联网指数分析
须眉科技是互联网公司吗
互联网经济与金融科技
亳州好赞互联网科技有限公司
黑云互联网科技有限公司
云南省自由互联网科技有限公司
张召忠谈中国互联网科技
广州梦谷互联网科技有限公司领导
互联网科技手机排行榜
万物互联网智能科技
互联网科技品牌
互联网下的科技发展
致聘互联网科技
深圳艾佳互联网科技
互联网科技公司名称
字节跳动和互联网科技
受益于互联网科技的高速发展
互联网科技公司实习生
中国互联网公司排名锤子科技
江苏苏星互联网科技有限公司
北京天晋互联网科技有限公司