第一步,开启定时任务开关
@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====!");
}
}
本文介绍如何在SpringBoot项目中配置并使用定时任务。首先需要在主类中通过@EnableScheduling注解开启定时任务功能,然后在业务类上使用@Component注解,并在具体的方法上使用@Scheduled注解来定义定时任务的具体行为。
755

被折叠的 条评论
为什么被折叠?



