Spring4.x整合Quartz2.x
Spring4.X整合Quartz2.X
1、引入pom依賴。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.iponkan</groupId> <artifactId>Spring-Quartz</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <spring-version>4.3.7.RELEASE</spring-version> <quartz-version>2.3.0</quartz-version> </properties> <dependencies> <!-- spring-tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring-version}</version> </dependency> <!-- spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring-version}</version> </dependency> <!-- spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring-version}</version> </dependency> <!-- spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring-version}</version> </dependency> <!-- spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring-version}</version> </dependency> <!-- quartz --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>${quartz-version}</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>${quartz-version}</version> </dependency> </dependencies> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </project> |
2、applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- 執行的類 --> <property name="targetObject"> <bean class="cn.iponkan.job.SecondJob"/> </property> <!-- 類中的方法 --> <property name="targetMethod"> <value>execute</value> </property> </bean> <!-- CronTriggerBean 觸發器 支援到指定時間執行一次,如每天12:00執行一次等。 --> <bean id="cronTrigger_2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean <property name="jobDetail"> <ref bean="jobDetail_2" /> </property> <!-- 每一秒鐘執行一次 --> <property name="cronExpression"> <value>0/1 * * * * ?</value> </property> </bean> <!-- 配置排程工廠 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 新增觸發器 --> <property name="triggers"> <list> <ref bean="cronTrigger_1" /> <ref bean="cronTrigger_2" /> </list> </property> </bean> </beans> |
3、Web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:*.xml </param-value> </context-param> <!-- Spring 容器啟動監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> |
4、編寫類。
package cn.iponkan.job; import java.util.Date; public class FirstJob { } |
package cn.iponkan.job; import java.util.Date; public class SecondJob { |
5、完整專案程式碼下載:https://download.csdn.net/download/qq_36135928/10636871
Spring4.x整合Quartz2.x時出現的一系列錯誤:
1、
Caused by: java.lang.NoClassDefFoundError: org/quartz/impl/JobDetailImpl |
因為Spring版本和Quartz版本不對應,spring3.1.x要使用quartz1.x版本。 |
2、
Cannot find class [org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean] for bean with name 'jobDetail_1' defined in file [D:\apache-tomcat-7.0.77\wtpwebapps\Spring-Quartz\WEB-INF\classes\applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1385) |
找不到類,引入如下配置: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring-version}</version> </dependency> |
3、
Cannot find class [org.springframework.scheduling.quartz.CronTriggerBean] for bean with name 'cronTrigger_1' defined in file [D:\apache-tomcat-7.0.77\wtpwebapps\Spring-Quartz\WEB-INF\classes\applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.scheduling.quartz.CronTriggerBean |
將CronTriggerBean修改為CronTriggerFactoryBean 補充: 還有的需要將SimpleTriggerBean修改為SimpleTriggerFactoryBean, 將JobDetailBean修改為JobDetailFactoryBean。 |
4、
Error creating bean with name 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' defined in file [D:\apache-tomcat-7.0.77\wtpwebapps\Spring-Quartz\WEB-INF\classes\applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/transaction |
加入spring的事務依賴: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring-version}</version> </dependency> |