Spring boot 实现定时任务

  1. 主类添加@EnableScheduling注解
  2. 实现类添加@Component
  3. 具体方法添加@Scheduled
@EnableScheduling
@SpringBootApplication
public class SpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootApplication.class);
    }
}

@Component
public class ScheduledTask {

    @Scheduled(cron = "0 * * * * ?")
    public void run() {
        System.out.println("现在时间:" + dateFormat.format(new Date()));
    }
}

Cron表达式在线生成工具