首先当然是搭建springboot工程,不做缀叙
在pom中导入Qyartz的依赖
因为springboot支持Qyartz,所以在创建项目的时候可以在这里选择:
创建定时任务:
1
2
3
4
5
6
7
8
9 1@Component
2public class HelloQuartz {
3 @Scheduled(cron = "0/5 * * * * ? ") //**注意这个地方,表示从0秒开始每5秒一次执行**
4 public void printCurrentTime(){
5 System.out.println("你是一个大傻瓜");
6 }
7}
8
9
在方法上加@Scheduled 注解 括号里的Cron表达式不建议手写,这里推荐一个在线生成网站 http://cron.qqe2.com/
任务创建完成在启动类上添加@EnableScheduling 表示开启任务
1
2
3
4
5
6
7
8
9
10
11 1@SpringBootApplication
2@EnableScheduling
3public class HelloQuartzApplication {
4
5 public static void main(String[] args) {
6 SpringApplication.run(HelloQuartzApplication.class, args);
7 }
8
9}
10
11
效果图: