spring boot 项目中定时器可以用 Scheduling
cron生成地址:[http://cron.qqe2.com/]
1.启动类中 加上@EnableScheduling
代码:
@SpringBootApplication
@Configuration
@EnableScheduling
@MapperScan(basePackages = {"com.test.dao"})
public class GeneralProjectApplication {
public static void main(String[] args) {
SpringApplication.run(GeneralProjectApplication.class, args);
}
}
2.自定义定时器类 需要加上@Component 注解 , 方法上加上@Scheduled 注解才能生效。
代码:
@Component
public class ConfirmReceivedTask {
/**
* 每天执行超过7天未收货的数据
* 每天 12点执行 0 00 00 ? * *
* 每天3:05执行 0 05 03 ? * *
* 本地测试:2分钟后执行(0 2/1 * * * ?)
*/
@Scheduled(cron = "0 0 0 * * ?")
public void confirmReceived() {
logger.info("==========要写的业务逻辑==========");
}
}
本文详细介绍如何在SpringBoot项目中使用@EnableScheduling启用定时任务,通过@Scheduled注解实现定时器类的方法调用,包括每天固定时间执行任务的具体配置示例。

1690

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



