1、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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 方式二:使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法-->
<!-- 定义目标bean和bean中的方法 -->
<bean id="job1" class="com.kexq.common.quartz.springQuartz.JobDemo1"/>
<bean id="method" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="job1"/>
<property name="targetMethod" value="execute"/> <!-- 要执行的方法名称 -->
<property name="concurrent" value="false" /> <!--将并发设置为false-->
</bean>
<!-- ======================== CronTriggerBean调度触发器 ======================== -->
<bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="method"></property>
<property name="cronExpression" value="0 0/1 * * * ?"></property>
</bean>
<!--======================== SimpleTriggerFactoryBean调度触发器 ======================== -->
<!-- <bean id="initSyncJob" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="SpringQtzJobMethod"/>
<property name="repeatInterval">
<value>2000</value> 2秒调度一次
</property>
<property name="startDelay">
<value>0</value>
</property>
重复次数:0
去掉的话按结束时间算
<property name="repeatCount">
<value>0</value>
</property>
</bean> -->
<!-- ======================== 调度工厂 ======================== -->
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="SpringJobSchedulerFactoryBean" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- 此处可以有多个定时器 <ref bean=""/>-->
<ref bean="CronTriggerBean"/>
</list>
</property>
</bean>
</beans>
注:触发器实现类参考:https://www.cnblogs.com/drift-ice/p/3817269.html
2、实体类
/**
* 该方法不用继承或实现任何类
*/
public class JobDemo1 {
// 调用的方法
public void execute() {
// 需要做的事情
System.out.println("基于MethodInvokingJobDetailFactoryBean的quartz任务");
}
public static void main(String[] args) {
System.out.println("----begin---");
ApplicationContext context = new ClassPathXmlApplicationContext("/quartz/spring-quartz1.xml");
System.out.println(context);
// 如果配置文件中将SpringJobSchedulerFactoryBean bean的lazy-init设置为false 则不用实例化
context.getBean("SpringJobSchedulerFactoryBean");
}
}