1.在类上添加注解
@Component
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
2.在类中方法上添加注解
@Scheduled(cron = "0 0 19 * * ?") //晚上七点执行一次。需要比如五秒执行一次,或者是某个时间点执行一次,请自行查,这里不做过多解释
效果如下图
@Component
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
public class ControllerTImeService {
//晚上七点执行一次。
//方法名可以随便写,但是要规范。
@Scheduled(cron = "0 0 19 * * ?")
public void selectCountDetail() throws IOException {
执行方法
}
//一个类中可以有多个定时方法。
}
项目启动后,方法就会根据时间点自动执行