您的位置:首页 > 编程语言 > Java开发

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

2015-10-23 22:50 549 查看
今天在练习使用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,顺利通过。
但是问题还是没有解决,为什么要求的类与原类一致却仍然说要进行转换呢?目标类型为什么与类内自己定义的类型不一致呢?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  BUG Spring