参考资料:https://www.springcloud.cc/spring-boot.html
中文文档2:https://felord.cn/_doc/_springboot/2.1.5.RELEASE/_book/index.html
1. 导入依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>
|
2. 任务类
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.demo.quartz;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Date;
@Component public class MyQuartz { @Scheduled(cron="*/2 * * * * ? ") public void testQuartz(){ System.out.println("testQuartz:"+new Date().toLocaleString()); } }
|
3. @EnableScheduling
在启动类添加 @EnableScheduling
配置,然后直接启动测试。
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.demo;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication @EnableScheduling public class Springboot02Application { public static void main(String[] args) { SpringApplication.run(Springboot02Application.class, args); } }
|