首先在SpringBoot 启动类上添加注解
@EnableScheduling
然后在自己定义一个schedule类,并注册进Bean容器
通过cron表达式实现实现定时任务
//cron = "0/5 * * * * ?" 表示每五秒执行一次
@Scheduled(cron = "0/5 * * * * ?")
public void test(){
System.out.println("-------------------------"+ new Date());
}
结果:
cron表达式可以通过在线工具生成在线Cron表达式生成器