Spring boot 实现定时任务
- 主类添加
@EnableScheduling注解 - 实现类添加
@Component - 具体方法添加
@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()));
}
}