如果我们需要每天定时进行财务汇总,我们的服务器就需要开启线程定时触发,SpringBoot中开启定时任务依然是使用注解。
一、首先在Application中添加注解
@EnableScheduling
二、写一个定时任务的类,使用注解设置类和方法即可
@Component
public class StartManager {
@Scheduled(fixedDelay = 1000 * 5)
public void start() {
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
String time = sdf.format(System.currentTimeMillis());
MsgUtils.show("定时记录:" + time);
}
}
运行效果:
其中cron表达式的相关规则参考这篇文章: