这也是我在学习的时候找到的,我就是按照人家的步骤,敲了一下,感觉理解了一些,希望对大家有所帮助,只是方便以后查找
1
package com.test.anotation;
public class EBankJobBean {
public void printAnotherMessage(){
System.out.println("CronTriggerBean 调用的定时任务..");
}
}
2
package com.test.anotation;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class EBankJob extends QuartzJobBean {
private EBankJobBean ebankJobBean;
public void setEbankJobBean(EBankJobBean ebankJobBean) {
this.ebankJobBean = ebankJobBean;
}
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
ebankJobBean.printAnotherMessage();
}
}
3.
package com.test.anotation;
public class MyJobBean {
public void printMessage(){
System.out.println("SimpleTriggerFactoryBean 调用定时任务");
}
}
4.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"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.test.anotation" />
<bean id="myJobBean" class="com.test.anotation.MyJobBean"></bean>
<bean id="eBankJobBean" class="com.test.anotation.EBankJobBean"></bean>
<bean id="simpleJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myJobBean" />
<property name="targetMethod" value="printMessage" />
</bean>
<bean name="complexJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="eBankJobBean" />
<property name="targetMethod" value="printAnotherMessage" />
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="simpleJobDetail" />
<property name="startDelay" value="1000" />
<property name="repeatInterval" value="3000" />
<!-- <property name="cronExpression" value="0 07 15 ? * *" /> -->
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="complexJobDetail" />
<property name="cronExpression" value="0 08 15 ? * *" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="simpleJobDetail" />
<ref bean="complexJobDetail" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>
5
package com.test.anotation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StratUpQuartzJobs {
static final Log log = LogFactory.getLog(StratUpQuartzJobs.class);
public static void main(String[] args) {
log.info("开始启动定时任务....");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
log.info("定时任务启动成功....");
}
}