原文地址: http://blog.sina.com.cn/s/blog_4cd3174a01000avb.html
使用Spring,不一定要继承QuartzJobBean类来定义一个类,
org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean直接指定执行某个对象的方法。
public class DemoJob{
private JobData jobData;
public JobData getJobData() {
return jobData;
}
public void setJobData(JobData jobData) {
this.jobData = jobData;
}
public void execute(){
System.out.println(jobData.getData() + "is executed");
}
}
<?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-2.0.xsd">
<bean id="someData" class="onlyfun.caterpillar.JobData"></bean>
<bean id ="someJob" class="onlyfun.caterpillar.DemoJob">
<property name="jobData">
<ref bean="someData" />
</property>
</bean>
<bean id="jobDetailBean" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="someJob"/>
</property>
<property name="targetMethod">
<value>execute</value>
</property>
</bean>
<bean id="cronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobDetailBean"></ref>
</property>
<property name="cronExpression">
<value>50 11 12 * * ?</value>
</property>
</bean>
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTriggerBean"/>
</list>
</property>
</bean>
</beans>