在启动类上开启定时任务:
@EnableScheduling
测试类
@Component
public class TestSchedule {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
//表达式
@Scheduled(cron = "0/5 * * * * ?")
//或直接指定时间间隔,例如:5秒
//@Scheduled(fixedRate=5000)
public void reportCurrentTime() {
System.out.println("现在时间:" + dateFormat.format(new Date()));
}
}
定时任务表达式详细配置:https://mp.csdn.net/postedit/90339800