spring集成quartz项目做定时任务,但是启动tomcat报错:
ClassNotFoundException: org.springframework.scheduling.quartz.CronTriggerBean…………
查阅资料,得知spring和quartz存在版本不兼容的问题,如果引入的quartz依赖是1.8.6以及以前的版本,依赖类是:
org.springframework.scheduling.quartz.CronTriggerBean
在2.xx版本之后,依赖类改为了:
org.springframework.scheduling.quartz.CronTriggerFactoryBean
由于引入的版本是2.x.x版本,所以修改了依赖类:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- guxf add -->
<bean id="propertyConfigurer" class=" com.ccb.ipsp.tools.PropertyUtils">
<property name="locations">
<list>
<value>classpath:ftp.properties</value>
</list>
</property>
</bean>
<!-- Quartz配置 配置定时任务 定时到服务器读取txt文件 -->
<!-- 任务1_定时到服务器路径下下载txt文件 -->
<bean name="job1" class="com.ccb.ipsp.controller.merchant.FtpFile" />
<bean id="jobDetail_1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="job1" />
</property>
<property name="targetMethod">
<value>Test</value>
</property>
</bean>
<!-- 需注意版本问题 -->
<!-- 1.8.6及以前版本:org.springframework.scheduling.quartz.CronTriggerBean -->
<!-- 2.x.x版本依赖类:org.springframework.scheduling.quartz.CronTriggerFactoryBean -->
<bean id="cronTrigger_1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="jobDetail_1" />
</property>
<property name="cronExpression">
<value>0 */2 * * * ?</value> <!-- 16:10分进行下载 -->
</property>
</bean>
<!-- 总配置 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 添加触发器 -->
<property name="triggers">
<list>
<ref bean="cronTrigger_1" />
</list>
</property>
</bean>
</beans>
配置了简单的测试类:
package com.ccb.ipsp.controller.merchant;
public class FtpFile {
public static void Test(){
System.out.println("===================");
}
}
启动tomcat,不报版本不兼容的错,也正常输出: