在需要执行的函数位置写入注解@Scheduled
@Component
public class taskController {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 3000) //单位为毫秒
public void runScheduled(){
System.out.println("当前时间:"+dateFormat.format(new Date()));
}
运行时,需要在SpringBootApplication文件中加入
@EnableScheduling

本文介绍如何在Spring Boot应用中使用@Scheduled注解创建定时任务。通过在需要执行的函数前添加此注解,并指定固定的时间间隔,可以轻松实现定时任务的调度。文章还提到了在Spring Boot启动类中启用定时任务的支持。
852

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



