在使用SimpleJdbcDaoSupport作为基类创建Dao时候出现的奇怪的BUG

在使用Spring框架的SimpleJdbcDaoSupport时遇到类型转换异常的问题,原本期望的SimpleJdbcTemplate类型被错误地要求转换为JdbcTemplate类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在练习使用SimpleJdbcDaoSupport时候,出现了一个莫名奇妙的BUG,现记录如下:

XML中配置如下:

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
		<constructor-arg ref="dataSource"/>
	</bean>
	
	<!-- <bean id="testDaoImpl" class="com.zdz.dao.TestDaoImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean> -->
	
	<bean id="jdbcTestDaoImpl" class="com.zdz.dao.JdbcTestDaoImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean>

其中JdbcTestDaoImpl类继承了SimpleJdbcDaoSupport:

public class JdbcTestDaoImpl extends SimpleJdbcDaoSupport

但是在使用时出现错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTestDaoImpl' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.springframework.jdbc.core.simple.SimpleJdbcTemplate' to required type 'org.springframework.jdbc.core.JdbcTemplate' for property 'jdbcTemplate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.jdbc.core.simple.SimpleJdbcTemplate] to required type 

说是从org.springframework.jdbc.core.simple.SimpleJdbcTemplate转换到org.springframework.jdbc.core.JdbcTemplate类的异常,但是查看SimpleJdbcDaoSupport 的源代码可见其属性simpleJdbcTemplate的类正是org.springframework.jdbc.core.simple.SimpleJdbcTemplate,就是上面所说的需要转换之前的类所以并不需要类型转换。于是我在XML中将要注入的Bean  jdbcTemplate的类型改为转换的目标类org.springframework.jdbc.core.JdbcTemplate,顺利通过。

但是问题还是没有解决,为什么要求的类与原类一致却仍然说要进行转换呢?目标类型为什么与类内自己定义的类型不一致呢?


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值