1 先要配置appliaction-task.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" default-autowire="byName" default-lazy-init="false"> <description>spring task定时任务</description> <!-- 定时任务配置 scheduler 方式 注解 暂时不支持动态更新 --> <context:component-scan base-package="com.leimingtech.admin.time" /> <task:executor id="executor" pool-size="5" /> <task:scheduler id="scheduler" pool-size="10" /> //线程池 <task:annotation-driven executor="executor" scheduler="scheduler" />//开启定时注解 </beans>
2 将appliaction-task.xml引到spring总体配置文件中
3 在项目中@Scheduled(cron="0 0 6 * * ?") 开启注解
// 每隔5秒执行一次:*/5 * * * * ? // 每隔1分钟执行一次:0 */1 * * * ? // 每天23点执行一次:0 0 23 * * ? // 每天凌晨1点执行一次:0 0 1 * * ? // 每月1号凌晨1点执行一次:0 0 1 1 * ? // 每月最后一天23点执行一次:0 0 23 L * ? // 每周星期天凌晨1点实行一次:0 0 1 ? * L // 在26分、29分、33分执行一次:0 26,29,33 * * * ? // 每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ? //任务执行时间设置 10分钟一次
注意:暂不支持在controller中执行定时任务