SpringBoot原生定时任务,不需要引入任何依赖
==只要了解,几个注解就可以使用==
- 1.在启动类上加入@EnableScheduling标签
- 2.在定时任务方法上加入@Schedule(fixedDelay=5000)
- 3.就是如此简单,简单的不可想象
package zebra.shjf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class TestQuartzApplication {
public static void main(String[] args) {
SpringApplication.run(TestQuartzApplication.class, args);
}
}
@Component
public class ScheduledTasks{
@Scheduled(fixedDelay = 5000)
public void execute() {
System.out.println("当前时间:" + new Date());
}
}