第一种方式
首先在配置文件头部必须要有:
xmlns:task="http://www.springframework.org/schema/task"
其次 xsi:schemaLocation 必须为其添加:
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
然后 spring 扫描过程必须涵盖定时任务类所在的目录:
<context:component-scan base-package="com.xx.xx" />
com.xx.xx 属于定时任务类的父级甚至更高级
然后设置动作启用定时任务
<task:annotation-driven/>
最后设置任务类
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@Lazy(value=false)
public class MyQuartzs {
@Scheduled(cron = "*/5 * * * * ?")//每隔5秒执行一次
public void test() throws Exception {
System.out.println("Test is working......");
}
//@Scheduled(cron = "0 0 1 * * ?")//每天凌晨1点整
//@Scheduled(cron = "0 30 0 * * ?")//每天凌晨0点30分
//@Scheduled(cron = "0 */60 * * * ?")//1小时处理一次
}
第二种方式
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd" default-autowire="byName"> <bean id="Job" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <!-- 1.你的定时任务所在的类 --> <bean class="com.reformer.xbgeo.controller.DataSyncController"/> </property> <property name="targetMethod"> <!-- 2.你的定时任务方法 --> <value>cleanInHistoryRecord</value> </property> </bean> <bean id="cleanInHistoryCron" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="Job" /> </property> <property name="cronExpression"> <!-- 3.设置你的定时器cron表达式 --> <value>0 0 1 * * ?</value> </property> </bean> <!-- 管理触发器 --> <bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="cleanInHistoryCron"/> </list> </property> </bean> </beans>
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于