在Application中开启注解
@EnableAsync
@EnableScheduling
@SpringBootApplication
public class Daka1Application {
public static void main(String[] args) {
SpringApplication.run(Daka1Application.class, args);
}
}
实列
@Service
public class ScheduledService {
@Scheduled(cron="0 24 12 * * ?")
public void hello(){
System.out.println("你被执行了");
}
}
cron表达式列子
每隔5秒执行一次:*/5 * * * * ?
每隔1分钟执行一次:0 */1 * * * ?
每天23点执行一次:0 0 23 * * ?
每天凌晨1点执行一次:0 0 1 * * ?
每月1号凌晨1点执行一次:0 0 1 1 * ?
每月最后一天23点执行一次:0 0 23 L * ?
每周星期天凌晨1点实行一次:0 0 1 ? * L
在26分、29分、33分执行一次:0 26,29,33 * * * ?
每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?