1.ApplicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- jobDao和jobBiz -->
<bean id="jobDao" class="dao.impl.JobDaoImpl"></bean>
<bean id="jobBiz" class="biz.impl.JobBizImpl">
<property name="jobDao" ref="jobDao"></property>
</bean>
<!-- 配置任务:jobDetail:jobClass/jobDataMap -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="job.JobSpring"></property>
<property name="jobDataMap">
<map>
<entry key="jobBiz" value-ref="jobBiz"></entry>
<entry>
<key>
<value>uname</value>
</key>
<value>Tom</value>
</entry>
</map>
</property>
</bean>
<!-- 配置触发器 -->
<!-- <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="startDelay" value="1000"></property>
<property name="repeatInterval" value="3000"></property>
<property name="jobDetail" ref="jobDetail"></property>
</bean> -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="cronExpression" value="0-20,40-59 * * * * ?"></property>
<property name="jobDetail" ref="jobDetail"></property>
</bean>
<!-- 配置调度器 -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
</beans>
2.job/JobSpring类
package job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import biz.JobBiz;
public class JobSpring extends QuartzJobBean {
private String uname;
private JobBiz jobBiz;
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext)
throws JobExecutionException {
System.out.println("========执行定时任务========");
jobBiz.jobBiz();
System.out.println(jobExecutionContext.getJobDetail().getJobDataMap().get("uname"));
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public JobBiz getJobBiz() {
return jobBiz;
}
public void setJobBiz(JobBiz jobBiz) {
this.jobBiz = jobBiz;
}
}
3.Test类
package test;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import job.RemaindJob;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.calendar.AnnualCalendar;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
}