方法一:使用注解@EnableScheduling开启定时任务
a.在启动页上面加上注解:
@EnableScheduling //开启定时任务
b.写一个新的class类:
@Component
public class TestTask {
private static final SimpleDateFormat dateformat = new SimpleDateFormat("HH:mm:ss");
//定义每过3秒执行任务
@Scheduled(fixedDelay = 3000)
public void reportCurrent(){
log.info("[现在时间],"+dateformat.format(new Date()));
}
}
方法二:使用cron表达式
a.同上
b.写一个新的class类:
@Component
public class TestTask {
private static final SimpleDateFormat dateformat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(cron = "1-10 * * * * ? ")
public void reportCurrent(){
log.info("[现在时间],"+dateformat.format(new Date()));
}
}