1.引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
2.在启动类上添加@EnableScheduling注解
3.创建定时任务
@Component
public class ScheduleTest {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Scheduled(cron = "0/2 * * * * *")
public void print(){
System.out.println("定时任务执行:"+sdf.format(new Date()));
}
}
4.启动项目,这个打印就会两秒执行一次