前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Quartz异常-Error creating bean with name ‘org.springframework.scheduling.quartz.SchedulerFactoryBean「建

Quartz异常-Error creating bean with name ‘org.springframework.scheduling.quartz.SchedulerFactoryBean「建

作者头像
全栈程序员站长
发布2022-08-31 19:42:19
9500
发布2022-08-31 19:42:19
举报

大家好,又见面了,我是你们的朋友全栈君。

目录

一、问题描述

二、问题分析

三、问题解决

四、工程源码

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

Quartz异常-Error creating bean with name 'org.springframework.scheduling.quartz.SchedulerFactoryBean「建议收藏」
Quartz异常-Error creating bean with name 'org.springframework.scheduling.quartz.SchedulerFactoryBean「建议收藏」

二、问题分析

从异常信息最后我们可以看到org/springframework/transaction/TransactionException,居然报的是事务异常,原来Quartz需要事务支持也就是spring-tx包,而我恰好没有导入这个包,所以导致实例化错误

三、问题解决

在Maven中加入spring-tx包,然后重新启动服务器即可

四、工程源码

4.1maven配置

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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配置文件

代码语言:javascript
复制
<?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.xsd
						http://www.springframework.org/schema/mvc
						http://www.springframework.org/schema/mvc/spring-mvc-.xsd
						http://www.springframework.org/schema/task  
						http://www.springframework.org/schema/task/spring-task.xsd
						http://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配置文件

代码语言:javascript
复制
<?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 定时任务类

代码语言:javascript
复制
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「建议收藏」
Quartz异常-Error creating bean with name 'org.springframework.scheduling.quartz.SchedulerFactoryBean「建议收藏」

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/142354.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、问题描述
  • 二、问题分析
  • 三、问题解决
  • 四、工程源码
    • 4.1maven配置
      • 4.2 web.xml
        • 4.3 spring配置文件
          • 4.4 Quartz配置文件
            • 4.5 定时任务类
              • 4.6 工程总体结构
              相关产品与服务
              容器服务
              腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档