一、applicationContext.xml文件的配置:
可单独写一个applicationContext-quartz.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"
default-autowire="byName" default-lazy-init="true">
<!-- BaseQuarez是创建的一个类,bean就是一个对象,quartzJob就是给这个对象起的名字,
用来实现定时器要做的事情 -->
<bean id="quartzJob" class="com.hzwu.quartz.BaseQuartz"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="testTask"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<!-- ref表示引用 quartzJob这个对象,也就是com.iflytek.quartz.BaseQuartz这个类-->
<ref bean="quartzJob" />
</property>
<!-- 调用BaseQuartz类中的方法 -->
<property name="targetMethod">
<value>updateQuartzState</value>
</property>
</bean>
<!-- 调度触发器 -->
<bean id="jibDoTime"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<!-- ref表示引用 testTask这个对象,依次递进,一步一步加控制-->
<ref bean="testTask" />
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<!-- <value>0 0 1 * * ?</value> -->
<value>0/1 * * * * ?</value>
</property>
</bean>
<!-- 调度工厂 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="jibDoTime" />
</list>
</property>
<!--必须的设置 QuartzScheduler 延时启动,应用启动完后 QuartzScheduler 再启动 -->
<property name="startupDelay" value="5" />
</bean>
</beans>
二、web.xml配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-quartz.xml</param-value>
</context-param>
三、编写BaseQuartz.java类
package com.hzwu.quartz;
public class BaseQuartz {
public void updateQuartzState() {
System.out.println("hello");
}
}