在启动类上加上@EnableScheduling注解
@SpringBootApplication @EnableScheduling //开启定时功能的注解,放在主入口 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
写定时任务
/** * 间隔3秒执行一次 */ @Scheduled(cron = "0/3 * * * * ? *") public void sayHelloJob() { System.out.println("Scheduled-sayHelloJob:Hello World"); }