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("==========要写的业务逻辑==========");
}
}