第一步,开启定时任务开关
@SpringBootApplication
@MapperScan("cn.guet.dao")
// 开启定时任务开关
@EnableScheduling
public class invoicingApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(invoicingApplication.class, args);
}
}
第二步,在想要定时的方法上标注@Scheduled,注意类也要标注@Component,不然Spring管不到
@Component
public class Result {
// 每隔一秒打印一次
@Scheduled(cron = "0/1 * * * * ?")
public void print(){
System.out.println("====sfgsg====!");
}
}