给dao层注入jdbcTemplate时的一个强行bug(jdbcDaoSupport不要随便用!用了要记得!)

本文记录了一个在Spring项目中由于Dao层不当继承导致的错误及解决过程。作者在配置数据源和JdbcTemplate时遇到了异常,最终发现是因为Dao层类错误地继承了JdbcDaoSupport并同时注入了自己的JdbcTemplate。

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

记录Dao层一个鱼唇至极的错误

这一天我在使用Spring的进行注解配置项目时,

我的Idea给我抛了一个如下的错误:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountDaoImpl' defined in file [D:\ideaworks\day53_spring4\demo03\target\classes\com\jxk\dao\impl\AccountDaoImpl.class]: Invocation of init method failed; 
nested exception is java.lang.IllegalArgumentException: 
'dataSource' or 'jdbcTemplate' is required

一开始看到这个错误,我赶紧又看了一下我的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans                                         http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 开启spring对注解事务的支持 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入DataSource -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 配置spring创建容器时要扫描的包 -->
    <context:component-scan base-package="com.jxk"></context:component-scan>

    <!-- 配置JdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置spring提供的内置数据源 -->
  <!--  <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/springdb1"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>-->
    
    <!--C3p0数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/springdb1"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
</beans>

感觉没啥毛病啊,难道是spring提供的内置数据源有问题?

于是我就把上边的数据源替换成c3p0的…...

一运行––-—--

'dataSource' or 'jdbcTemplate' is required

难道是注解有干扰?于是在@Autowired下边添加了一个@Qualifier

@Repository
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
    @Autowired
    @Qualifier(value = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

但还是没有效果..….

但其实,bug就在上边的几行代码里…....

因为copy-paste的原因...我的Dao层实例竟然继承了一个JdbcDaoSupport类!!!

继承这个类的话就可以使用它内置的一个getJdbcTemplate方法,而不用再自己创建一个jdbcTemplate属性.….但是这个继承这个类以后,再去创建自己的jdbcTemplate会怎么样呢?恭喜我,成功为自己制造了一个bug..….

如果要使用注释注入,就不要在继承这个类了呀!!!,

报错的原因正是

我给AccountDaoImpl注入了jdbcTemplate,但它继承的父类JdbcDaoSupport里边的jdbcTemplate却还是空的!!!!

如果非要作,还是要继承这个类的话,可以这样!!!:

@Repository
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //在这里,给dao层实例的父类的jdbcTemplate也赋值!!!
    @Autowired
    private void  setSuperDataSources(ComboPooledDataSource dataSources){
       super.setDataSource(dataSources);
    }

copy一时爽!!!!!一直copy一直爽!!!

啊!我的时间!我的头发!

转载于:https://www.cnblogs.com/mysetsuna/p/10621234.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值