1、cron表达式在线生成网址
cron.qqe2.com/
2、写相关定时任务配置代码
import com.imooc.utils.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class OrderJob {
@Scheduled(cron = "0/3 * * * * ? ")
public void autoCloseOrder(){
System.out.println("执行定时任务,当前时间为:"
+ DateUtil.getCurrentDateString(DateUtil.DATE_PATTERN));
}
}
3、开启定时任务
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}