化身天使的博客

8.spring定时任务

Scheduled

单线程

开启定时任务功能,
启动类加注解@EnableScheduling

在定时任务文件里,类上加@Component注解,方法上加@Scheduled注解

@Component
public class Tes {
    @Scheduled(cron="0 0 7 * * * *")   //每天7点执行
    private void taskTest(){}
}

多线程

参数表达式说明

cron

类似linux的crontab, 在最前面加了个秒,在最后面加了个年
秒 分 时 日 月 周 年
@Scheduled(cron="0 0 7 * * * *")

fixedRate

周期性执行, 每隔多长时间执行一次,单位毫秒,一秒等于1000毫秒
@Scheduled(fixedRate=1000)

posted @ 2024-01-25 19:51  化身天使  阅读(20)  评论(0)    收藏  举报