springboot使用@Schednled 注解实现定时任务

part 1:

@Component
public class Scheduled {
    SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    
    @Scheduled(cron = "0 0/30 *  * * ? ")   //每隔30分钟执行一次
    public void timerRate() {
        System.out.println(dateFormat.format(new Date()));
      
        //编写 需要定时执行的业务就OK了
        aaa(User);
    }
}

part 2:
    在SpringBootApplication 类,上加注解@EnableScheduling


@SpringBootApplication
@EnableScheduling
public class SpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootScheduledApplication.class, args);
    }
}


完成以上两个步骤,springboot 定时任务OK了!

原文地址:https://www.cnblogs.com/zongheng14/p/9008544.html