1. 第一步:启动类上加上注解,表示开启定时任务
@SpringBootApplication
@EnableScheduling // 开启定时任务
public class StaApplication {
public static void main(String[] args) {
SpringApplication.run(StaApplication.class,args);
}
}
2. 创建定时任务类
创建定时任务类,在这个类里面使用表达式设置什么时候去执行
1. cron表达式,设置执行的规则
@Component
public class ScheduledTask {
/**
* 0/5 * * * * ?:每隔5秒执行一次这个方法
* 虽然称为七子表达式,实际是6位,因为默认是当前年
*/
@Scheduled(cron = "0/5 * * * * ?")
public void tasjk1(){
System.out.println("task执行了***************");
}
}
corn表达式设置执行规则
也称之为 七子表达式(七域表达式)