SpringBoot - 实现定时任务
1.在启动类,加入@EnableScheduling 注解
@SpringBootApplication @EnableScheduling public class SpringbootScheduleApplication { public static void main(String[] args) { SpringApplication.run(SpringbootScheduleApplication.class, args); } }
2. @Scheduled 注解实现定时任务
@Component public class TaskService { @Scheduled(cron = "xxxxxx") public void task() { System.out.println("Thread Name : " + Thread.currentThread().getName() + " i am a task : date -> " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); } }