在启动类上添加 @EnableScheduling
@EnableScheduling
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Component
public class SchedulerTaskTest {
@Scheduled(fixedDelay = 2000) //或者cron="*/2 * * * * ?"
public void fixedDelayJob() throws InterruptedException {
System.out.println("2S一次"+new Date());
}
}