首先在启动类AdminApplication 上加上注解 @EnableScheduling 用来开启定时任务。
创建一个类来定义定时任务,加上注解与corn表达式即可,也还可以选择每分钟定时运行或者此次未执行完毕则不进行下次定时任务,因为没有用到,这里不做记录,有需要的可以自行百度,注解里边增加对应参数即可。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.ParseException;
@Component
public class SchedulerTask {
/**
* 自动生成自动报告的后续报告
*/
@Scheduled(cron = "0 0 1 1/1 * ? ")
private void process(){
log.info("定时每天凌晨一点执行!")
}
}