这一天我在使用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的…...
一运行––-—--
难道是注解有干扰?于是在@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一直爽!!!
啊!我的时间!我的头发!
如对本文有疑问, 点击进行留言回复!!
Unity UGUI 之 实现按钮 Button 长按和双击的功能效果
[U3D Learning Note] Unity C# Surival Guide (1) -- Quick Tips and Assets
微服务(SpringCloud)与分布式(Dubbo)的区别你真的知道嘛?
网友评论