package com.synpower.quzrtz; import java.util.Date; import org.springframework.stereotype.Component; @Component public class TimedTasks { public void execute1() { for (int i = 0; i <1000; i++) { System.out.println(new Date() + "==================execute1"); } } public void execute2() { for (int i = 0; i <1000; i++) { System.out.println(new Date() + " -> execute2======"); } } }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 11 12 <bean id="taskDetail_1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 13 <!-- 执行的类 --> 14 <property name="targetObject"> 15 <ref bean="timedTasks" /> 16 </property> 17 <!-- 类中的方法 --> 18 <property name="targetMethod"> 19 <value>execute1</value> 20 </property> 21 </bean> 22 23 <bean id="cronTriggerBean_1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 24 <property name="jobDetail"> 25 <ref bean="taskDetail_1" /> 26 </property> 27 <!-- 每天凌晨一点执行一次 --> 28 <property name="cronExpression"> 29 <value>0 0 1 * * ?</value> 30 </property> 31 </bean> 32 <bean id="taskDetail_2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 33 <!-- 任务所属组 --> 34 <property name="group" value="job_work"/> 35 <!-- 任务所属组名字 --> 36 <property name="name" value="job_work_name"/> 37 <!--false表示等上一个任务执行完后再开启新的任务--> 38 <property name="concurrent" value="false"/> 39 <!-- 执行的类 --> 40 <property name="targetObject"> 41 <ref bean="timedTasks" /> 42 </property> 43 <!-- 类中的方法 --> 44 <property name="targetMethod"> 45 <value>execute2</value> 46 </property> 47 </bean> 48 <bean id="cronTriggerBean_2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 49 <property name="jobDetail"> 50 <ref bean="taskDetail_2" /> 51 </property> 52 <!-- 每天凌晨两点执行一次 --> 53 <property name="cronExpression"> 54 <value>0 0 2 * * ?</value> 55 </property> 56 </bean> 57 <!-- ======================== 调度工厂 ======================== --> 58 <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 59 <!-- 添加触发器 --> 60 <property name="triggers"> 61 <list> 62 <ref bean="cronTriggerBean_1" /> 63 <ref bean="cronTriggerBean_2" /> 64 </list> 65 </property> 66 </bean> 67 </beans>
然后在Springmvc中引入这个XML,重点来了
需要删掉web.xml的一段配置
这样quartz 就不会被Spring加载两次了。