spring mvc定时器的简单使用

1 在xml配置文件头部添加

xmlns:task="http://www.springframework.org/schema/task"

2 在xsi:schemaLocation中添加

http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task.xsd

3 添加包扫描路径

<context:component-scan base-package="com.xx.xx" />

4 开启定时任务注解驱动

<task:annotation-driven/>

5 执行定时任务的类和方法

@Component
public class TaskTimer {
    
    /**
     * 清理过期代金券(每天0点执行一次)
     *
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void cleanVoucher() {
        
    }

}

使用@Component注册组件,并使用@Scheduled(cron = "0 0 0 * * ?")开启定时任务

附录:

CronTrigger配置格式:

格式: [秒] [分] [小时] [日] [月] [周] [年]

序号 说明 是否必填 允许填写的值 允许的通配符 
1 秒 是 0-59 , - * /
2 分 是 0-59 , - * /
3 小时 是 0-23 , - * /
4 日 是 1-31 , - * ? / L W
5 月 是 1-12 or JAN-DEC , - * /
6 周 是 1-7 or SUN-SAT , - * ? / L #
7 年 否 empty 或 1970-2099 , - * /

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值