Spring JDBC:无法获取数据源 [英] Spring JDBC : unable to get dataSource

查看:234
本文介绍了Spring JDBC:无法获取数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为appConfig.xml的spring XML配置文件,其中包含一个数据源bean和另一个JDBC模板,数据源作为参数传递给该JDBC模板:

I have a spring XML config file called appConfig.xml which contains a datasource bean and another JDBCtemplate to which datasource is passed as an argument:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/test"/>
</bean>

<bean id="JDBCTemplate" class="com.myprojects.JDBCTemplate">
    <property name="dataSource" ref="dataSource"/>
</bean>

我得到一个错误:

线程主"中的异常org.springframework.beans.factory.BeanCreationException:创建文件[appConfig.xml]中定义的名称为'JDBCTemplate'的bean时出错:设置属性值时出错;嵌套的异常是org.springframework.beans.PropertyBatchUpdateException;嵌套的PropertyAccessExceptions PropertyAccessException 1:org.springframework.beans.MethodInvocationException:属性'dataSource'抛出异常;嵌套异常是java.lang.IllegalArgumentException:属性'dataSource'是必需的

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'JDBCTemplate' defined in file [appConfig.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'dataSource' threw exception; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required

从我从此错误中看到的内容来看,它无法将dataSource传递给JDBCTemplate.日志还显示:

From what I see from this error, it is not able to pass dataSource to JDBCTemplate. The logs also says:

已加载的JDBC驱动程序:com.mysql.jdbc.Driver

Loaded JDBC driver: com.mysql.jdbc.Driver

我在pom.xml中提到了所有依赖项,并验证了是否已加载spring所需的jar并已加载了mysql jdbc连接器.关于这个问题可能有什么线索?

I have all dependencies mentioned in pom.xml and I verified that necessary jars for spring are loaded and mysql jdbc connector is also loaded. Any clues on what the issue may be?

我尝试了一个不同的项目,在该项目中我手动添加了所有spring依赖项和mysql jdbc连接器作为库的一部分.那里工作正常.但是,当尝试通过pom.xml包含依赖项时,我遇到了这个问题.所以我假设这与某些依赖关系没有被拉进来有关.但无法从错误中找出哪一个.

I tried a different project where I manually added all spring dependencies and mysql jdbc connector as part of library. It worked fine there. But while trying to include dependencies via pom.xml, I'm facing this issue. So I'm assuming this is to do with some dependency not being pulled in or something. But unable to figure out which one from the error.

推荐答案

它认为您的类JDBCTemplate没有名为dataSource

It thinks your class JDBCTemplate does not have a property named dataSource

也许您没有一个具有正确类型的单个参数的公共方法setDataSource().

Perhaps you don't have a public method setDataSource() that has a single argument of the right type.

也许是私人的.

也许拼写错误.

这篇关于Spring JDBC:无法获取数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆