本文主要是介绍Quartz异常-Error creating bean with name 'org.springframework.scheduling.quartz.SchedulerFactoryBean,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
一、问题描述
二、问题分析
三、问题解决
四、工程源码
4.1maven配置
4.2 web.xml
4.3 spring配置文件
4.4 Quartz配置文件
4.5 定时任务类
4.6 工程总体结构
一、问题描述
今天使用Quartz+Spring测试定时任务时,发现报异常:Error creating bean with name 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' defined in class path resource [applicationContext-quartz.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/transaction/TransactionException
二、问题分析
从异常信息最后我们可以看到org/springframework/transaction/TransactionException,居然报的是事务异常,原来Quartz需要事务支持也就是spring-tx包,而我恰好没有导入这个包,所以导致实例化错误
三、问题解决
在Maven中加入spring-tx包,然后重新启动服务器即可
四、工程源码
4.1maven配置
<?xml version="1.0" encoding="UTF-8"?><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>com.codecoord</groupId><artifactId>QuartzTest</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>QuartzTest Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz --><dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId><version>2.3.0</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.1.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.1.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>4.1.6.RELEASE</version></dependency></dependencies><build><finalName>QuartzTest</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.0.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.7.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.20.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.0</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>
</project>
4.2 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>
4.3 spring配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:task="http://www.springframework.org/schema/task"xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aops.xsd"><!-- 导入quartz配置文件 --><import resource="classpath:applicationContext-quartz.xml"/>
</beans>
4.4 Quartz配置文件
<?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"><!-- 任务类 --><bean id="myJob" class="com.codecoord.main.QuartzTest"/><!-- 任务明细 --><bean id="task" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><!-- 作业类 --><property name="targetObject"><ref bean="myJob"/></property><!-- 作业类方法 --><property name="targetMethod"><value>myTask</value></property></bean><!-- 执行计划-触发器 --><bean id="jobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><!-- 执行任务 --><property name="jobDetail"><ref bean="task"/></property><!-- 制定执行周期 --><property name="cronExpression"><value>0/2 * * * * ?</value></property></bean><!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref bean="jobTrigger"/> </list></property></bean>
</beans>
4.5 定时任务类
package com.codecoord.main;public class QuartzTest {public void myTask() {System.out.println("This message is from Quartz!");}
}
4.6 工程总体结构
这篇关于Quartz异常-Error creating bean with name 'org.springframework.scheduling.quartz.SchedulerFactoryBean的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!