loading...
Spring 定时任务
Published in:2022-01-31 | category: Spring
Words: 205 | Reading time: 1min | reading:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Component
public class TaskDemo {

//1.创建一个maven项目
//2.添加Spring环境 ->导入Spring对应的jar包
//3.在Spring引导类上 添加@EnableScheduling注解 ,开启Spring的定时任务
//4.编写一个任务类,类中编写方法[执行的任务],方法上添加@Scheduled注解
//难点 cron是什么?
// 表达式。 解决的问题就是在什么时候执行任务
// 秒 分 时 日 月 星期
// *:任意, 秒 0-59 时 0-23
// ,: 10,15,20
// -: 10-20 从10秒到20秒中每秒执行一次
// /: 1/6 从1秒开始每隔6秒执行一次
// ?: 占位符 ,没有任何意义,日和星期两个之中必须有且仅有一个是?
@Scheduled(cron="1/6 * * * * ?")
public void aTask(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy:MM:dd hh:mm:ss");
System.out.println("============================>" + simpleDateFormat.format(new Date()));
}
}
Prev:
SpringCloud注册中心
Next:
Springboot之HandlerInterceptor拦截器(二)
catalog
catalog