1、核心接口
TaskExecutor
TaskScheduler
2、注解
//开启定时任务
@EnableScheduling
//什么时候执行
@Scheduled
主要代码如下:
@Service
public class ScheduledService {
// 在一个特定的时间执行这个方法
//cron表达式
//秒 分 时 日 月 周几
@Scheduled(cron = "0 10 16 * * ?")
public void hello() {
System.out.println("你被执行了");
}
}
//开启定时任务
@EnableScheduling
@SpringBootApplication
public class SpringSecurity01Application {
public static void main(String[] args) {
SpringApplication.run(SpringSecurity01Application.class, args);
}
}
cron表达式
- 30 15 10 * * ? 每天10点15分30秒执行一次
- 30 0/5 10,18 * * ? 每天10点和18点,每隔5分钟,在30秒的时候执行一次
- 0 15 10 ? * 1-6 每个月的周一到周六10点15分执行一次
…