spring集成quartz报数据库链接问题

本文档描述了在Spring中集成Quartz定时任务时遇到的数据库连接异常,异常信息提示无法打开JDBC连接进行事务处理。问题根源在于自动装配导致的PropertyPlaceholderConfigurer失效,使得${jdbc.driverClassName}等属性无法从properties文件中获取。解决方案是将SchedulerFactoryBean的autowire属性设置为"no",避免自动装配,确保属性配置正确读取。
摘要由CSDN通过智能技术生成
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName">

<bean id="quartzJob" class="com.study.spring.quartz.QuartzJob" />

<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="quartzJob" />
<property name="targetMethod" value="myJob" />
<property name="concurrent" value="false"/>
<!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
</bean>

<bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail"/>
<property name="cronExpression" value="*/1 * * * * ?" /><!-- 每隔一秒触发一次 -->
</bean>

<bean id="batchQuartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 此处可以配置其他属性,包括加载配置文件等 -->
<property name="triggers">
<list>
<ref bean="myTrigger"/>
</list>
</property>
</bean>
</beans>

=========================================================================
以上中定义batchQuartz的bean,启动服务时,一直报org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchQuartz' defined in class path resource [conf/spring/spring-quartz.xml]: Invocation of init method failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${jdbc.driverClassName}'

解决方式:
在定义batchQuartz时,配置 autowire="no",不让自动装配,否则PropertyPlaceholderConfigurer失效,也就是用${jdbc.username}这样之类的表达式,将无法获取到properties文件里的内容
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值