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 , - * /