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());
}
}
本文介绍如何在SpringBoot项目中实现简单且高效的定时任务。仅需引入@EnableScheduling注解并结合@Scheduled注解即可轻松创建固定延迟的任务。通过示例代码展示了如何每5秒打印当前时间。
427

被折叠的 条评论
为什么被折叠?



