第一步:引入依赖web
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.1.0</version>
</dependency>spring
注意:Spring3.1如下的版本必须使用quartz1.x系列,3.1以上的版本才支持quartz 2.x
个人spring版本是3.1以上的,因此这里引入2.X。这里须要注意下你的spring-context和spring-context-support已经导入且版本>3.1缓存
为何导入spring-context-support,解释以下:
<!--
含支持UI模版(Velocity,FreeMarker,JasperReports),邮件服务,脚本服务(JRuby),缓存Cache(EHCache),
任务计划Scheduling(quartz)方面的类。外部依赖spring-context, (spring-jdbc, Velocity, FreeMarker,
JasperReports, BSH, Groovy, JRuby, Quartz, EHCache)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>测试
第二步:建立spring-quart.xmlspa
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--定时器一:测试定时任务 1分钟-->
<!--定时任务所在类路径-->
<bean name="gpOrderFlush" class="com.service.UserService"/>
<bean id="jobDetail_1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="gpOrderFlush" />
</property>
<!--定时任务方法 本实例为UserService.task( )方法-->
<property name="targetMethod">
<value>task</value>
</property>
<property name="concurrent" value="false" />
</bean>
<!-- 若是quartz的版本 <2 那么此处是 org.springframework.scheduling.quartz.CronTriggerBean -->
<bean id="cronTrigger_1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="jobDetail_1" />
</property>
<!--定时任务间隔时间,此处为一分钟-->
<property name="cronExpression">
<value>0 0/1 * * * ?</value>
</property>
</bean>xml
<!-- 启动工做 -->
<bean id="SpringJobSchedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!--定时任务一启动-->
<ref bean="cronTrigger_1" />
</list>
</property>
<property name="autoStartup" value="true"></property>
</bean>
</beans>blog
第三步:在web.xml中引入ci
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-quartz.xml
</param-value>
</context-param>get
定时任务到此已经配置好了,启动项目观察控制台:it