gpt4 book ai didi

java - Spring JDBC : unable to get dataSource

转载 作者:行者123 更新时间:2023-12-01 23:41:34 26 4
gpt4 key购买 nike

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

<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>

我收到错误:

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。日志还说:

Loaded JDBC driver: com.mysql.jdbc.Driver

我拥有 pom.xml 中提到的所有依赖项,并且我验证了 spring 所需的 jar 已加载,并且 mysql jdbc 连接器也已加载。关于问题可能是什么的任何线索吗?

我尝试了一个不同的项目,其中我手动添加了所有 spring 依赖项和 mysql jdbc 连接器作为库的一部分。那里工作得很好。但是,在尝试通过 pom.xml 包含依赖项时,我遇到了这个问题。所以我假设这与某些依赖关系未被拉入或其他原因有关。但无法从错误中找出是哪一个。

最佳答案

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

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

也许它是私有(private)的。

可能是拼写错误。

关于java - Spring JDBC : unable to get dataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17841333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com