springmvc 执行@Test报错

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> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值