JAVA全系列 教程
3762个小节阅读:7089.8k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
我们也可以使用注解的方式配置定时任务:
在Spring配置文件中开启注解任务,使注解生效:
xxxxxxxxxx
<!-- 开启注解任务 -->
<task:annotation-driven />
在方法上方添加@Scheduled
,指定该方法定时执行
xxxxxxxxxx
// 定时任务类
@Component
public class MySpringTask2 {
// 打印时间
@Scheduled(cron = "* * * * * *")
public void printTime(){
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String now = sdf.format(new Date());
System.out.println(now+"定时任务2");
}
}
运行测试类
在配置配置文件开启注解任务:
在配置类上添加@EnableScheduling
注解