springboot定时任务

  1. 启动类加注解

@EnableScheduling  --- org.springframework.scheduling.annotation.EnableScheduling
2. 保证包被扫描到,可以加@ComponentScan(basepackage = {"com.java.test"})

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
//bean注入
@Component
public class DjtzScheduleTask {

  //开启任务,默认单线程
@Scheduled(cron = "0 1/1 * * * ?")
public void exportPerMonth(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("test" + simpleDateFormat.format(new Date()));
}

}
上一篇:Scheduling-tasks:spring的定时任务


下一篇:OS L4-1: Scheduling Overview