一、在application.yml中添加cron
scheduled:
cron:
test: "*/10 * * * * ?"
二、在启动类添加@EnableScheduling
@SpringBootApplication
@EnableScheduling
public class AlgApplication {
public static void main(String[] args) {
SpringApplication.run(AlgApplication.class, args);
}
}
三、测试
@Component
@Slf4j
public class TimeTask {
@Scheduled(cron = "${scheduled.cron.test}")
public void task() {
log.info("开始定时任务");
}
}