org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in class path resource [spring/bean.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.chen.ssm.dao.UserDao]: Specified class is an interface
......
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.chen.ssm.dao.UserDao]: Specified class is an interface
问题描述:不能实例化[com.chen.ssm.dao.UserDao]这个对象
解决方法:
1、查看问题提示:主要注意Caused by: 后的信息提示
2、UserDao是个接口,UserDaoImpl是实现类
public interface UserDao {
//根据id查询用户信息
public User findUserById(int id) throws Exception;
}
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
public User findUserById(int id) throws Exception {
// 继承SqlSessionDaoSupport,通过this.getSqlSession()得到sqlSession
SqlSession sqlSession = this.getSqlSession();
User user = sqlSession.selectOne("test.findUserById",id);
return user;
}
}
3、注意核心配置文件将两个类的连接
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="mybatis/SqlMapConfig.xml"/>
<property name="dataSource" ref="dataSource"/>
bean>
<bean id="userDao" class="com.chen.ssm.dao.UserDao">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
bean>
beans>
忽然记得以前配置spring-hibernate核心配置文件时,好像将它们之间的路径写错过,最后发现
<bean id="userDao" class="com.chen.ssm.dao.UserDao">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
bean>
里的class路径确实写错了,应该写成实现类的路径,修改如下
id="userDao" class="com.chen.ssm.dao.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>