springmvc 执行@Test报错

来源:互联网 发布:spss数据显著性分析 编辑:程序博客网 时间:2024/06/08 12:26
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、注意核心配置文件将两个类的连接

<?xml version="1.0" encoding="UTF-8"?>  <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"/>    <!-- 数据源,使用dbcp -->    <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>    <!-- sqlSessionFactory -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <!-- 加载mybatis的配置文件 -->        <property name="configLocation" value="mybatis/SqlMapConfig.xml"/>        <!-- 数据源 -->        <property name="dataSource" ref="dataSource"/>    </bean>     <!-- 原始dao接口 -->         <bean id="userDao" class="com.chen.ssm.dao.UserDao">        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>    </bean>     </beans>

忽然记得以前配置spring-hibernate核心配置文件时,好像将它们之间的路径写错过,最后发现

<!-- 原始dao接口 -->         <bean id="userDao" class="com.chen.ssm.dao.UserDao">        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>    </bean>     

里的class路径确实写错了,应该写成实现类的路径,修改如下

<bean id="userDao" class="com.chen.ssm.dao.UserDaoImpl">        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>    </bean> 
原创粉丝点击