2. 定义定时任务
import java.util.Date; public class TestTask { public void execTask() { System.out.println("TestTask#execTask was executed at" + new Date()); } }3. 测试类
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestQuartz { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new ClassPathXmlApplicationContext("applicationContext.xml"); } }4. Spring配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- 定义实际执行的任务 --> <bean id="testTask" class="task.TestTask"></bean> <!-- 定义定时任务 --> <bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 指定定时任务调度类 --> <property name="targetObject"><ref bean="testTask"/></property> <!-- 指定定时任务调度方法 --> <property name="targetMethod"><value>execTask</value></property> </bean> <!-- 定义定时任务触发器 --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="methodInvokingJobDetail"/> </property> <property name="cronExpression"> <value>0/5 * * * * ?</value> </property> </bean> <!-- 定义定时调度工厂 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list><ref local="cronTrigger"/></list> </property> </bean> </beans>