需要的包 quartz-all-1.6.0.jar
写个普通的类
public class TaskJob {
public void SayHello() {
try {
System.out.println("执行了SayHello方法");
} catch (Exception e) {
}
}
}
配置application.xml
<bean id="taskJob" class="com.bdqn.heidu.TaskJob" />
<bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="taskJob" />
</property>
<property name="targetMethod">
<value>SayHello</value>
</property>
</bean>
<!-- 配置触发器 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 这里不可以直接在属性jobDetail中引用taskJob,因为他要求的是一个jobDetail类型的对象,所以我们得通过MethodInvokingJobDetailFactoryBean来转一下 -->
<property name="jobDetail">
<ref bean="methodInvokingJobDetail" />
</property>
<!-- 每天的8点到21点每隔1分钟触发,具体说明见附录 -->
<property name="cronExpression">
<value>0 12 9 ? * *</value>
</property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 添加触发器 -->
<property name="triggers">
<list>
<ref local="cronTrigger" />
</list>
</property>
</bean>