使用junit测试ssh搭建的框架的时候,遇到了一个异常:

异常消息:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.htd.test.Test1':
Injection of resource dependencies failed;

nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Bean named 'registerService' must be of type [com.htd.service.impl.RegisterServiceImpl],
but was actually of type [com.sun.proxy.$Proxy42]

测试类代码:

 package com.htd.test;

 import javax.annotation.Resource;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import com.htd.domain.Customer;
 import com.htd.service.impl.RegisterServiceImpl;

 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration("classpath:applicationContext.xml")
 public class Test1 {
     @Resource(name="registerService")
     private RegisterServiceImpl registerService;

     @Test
     public void test1() {
         Customer customer=new Customer();
         customer.setCus_name("eastry");
         customer.setCus_email("123456@qq.com");
         customer.setCus_pwd("123");
         customer.setCus_id(1);
         registerService.save(customer);
     }

 }

RegisterServiceImpl类:

 package com.htd.service.impl;

 import org.springframework.transaction.annotation.Transactional;

 import com.htd.dao.impl.CustomerDaoImpl;
 import com.htd.domain.Customer;
 import com.htd.service.IRegisterService;

 @Transactional
 public class RegisterServiceImpl implements IRegisterService {
     //xml方式注入
     private CustomerDaoImpl customerDao;
     public void setCustomerDao(CustomerDaoImpl customerDao) {
         this.customerDao = customerDao;
     }
     @Override
     public void save(Customer customer) {
         customerDao.save(customer);
     }
 }

RegisterServiceImpl的接口IRegisterService:

 import com.htd.domain.Customer;

 public interface IRegisterService {
     public void save(Customer customer);
 }

运行结果:

不好,红了。。。。。


这里没有使用Cglib代理,Java的动态代理是使用接口实现的,但是在Test类里面注入的时候是使用Service的实现类(即:RegisterServiceImpl)注入的,所以这里会出错。

如图:

修改成下列代码测试成功:

修改后的测试类代码:

 import javax.annotation.Resource;

 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

 import com.htd.domain.Customer;
 import com.htd.service.IRegisterService;
 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration("classpath:applicationContext.xml")
 public class Test1 {
     @Resource(name="registerService")
     private IRegisterService registerService;

     @Test
     public void test1() {
         Customer customer=new Customer();
         customer.setCus_name("eastry");
         customer.setCus_email("123456@qq.com");
         customer.setCus_pwd("123");
         customer.setCus_id(1);
         registerService.save(customer);
     }
 }

运行结果:

嗯,变绿了,很好。。。。。。。。。。。


如果想用类注入,就要导入Cglib的jar包 使用类来实现代理????请大佬指导,我还没试试,之后会去实验下。


Spring异常——BeanNotOfRequiredTypeException的更多相关文章

  1. Spring异常抛出触发事务回滚

    Spring.EJB的声明式事务默认情况下都是在抛出unchecked exception后才会触发事务的回滚 /** * 如果在spring事务配置中不为切入点(如这里的切入点可以定义成test*) ...

  2. (转)spring异常抛出触发事务回滚策略

    背景:在面试时候问到事务方法在调用过程中出现异常,是否会传递的问题,平时接触的比较少,有些懵逼. spring异常抛出触发事务回滚策略 Spring.EJB的声明式事务默认情况下都是在抛出unchec ...

  3. spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet

    spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet   情况 ...

  4. Hibernate整合Spring异常'sessionFactory' or 'hibernateTemplate' is required

    今日在写GenericDao时,发现了一个异常,内容如下: org.springframework.beans.factory.BeanCreationException: Error creatin ...

  5. spring 异常记录

    1.异常: java.lang.IllegalArgumentException: No converter found for return value of type: class java.ut ...

  6. Spring异常累计(1)Spring注解与扫描,NoUniqueBeanDefinitionException

    spring中可以使用注解机制,代替传统的在xml中配置一个bean. 如 <pre name="code" class="java">@Compo ...

  7. spring 异常管理机制

    三.异常处理的几种实现: 3.1.在经典的三层架构模型中,通常都是这样来进行异常处理的: A.持久层一般抛出的是RuntiomeException类型的异常,一般不处理,直接向上抛出. B.业务层一般 ...

  8. spring异常处理器

    一.本篇文章旨在讨论异常处理器: 1.因为异常处理器在实战中通常用来处理开发人员自定义的运行时异常,所以如果要了解如何自定义运行时异常,请自行搜索相关资料. 2.本文的demo用IndexOutOfB ...

  9. spring异常

    1.The type org.springframework.core.NestedRuntimeException cannot be resolved. It is indirectly refe ...

随机推荐

  1. gvim 常用键

    按i 切换到插入模式,ESC退出 命令模式下 dd 删除一行 dw 删除一个字 x 删除一个字符 :set num  设置行号 :reg 查看寄存器 p 粘贴 y 复制 "*p  不同环境下 ...

  2. background-size属性

    background-size:属性有 auto:length :百分比 length 如:10px 20px 固定的 或者是写成一个 ,10px  另外一个就默认为 auto; 写成百分比的形式 是 ...

  3. Wolfycz的娱乐赛题解

    现在不会放题解的!比赛完了我会把题解放上来的 祝大家玩的愉快~ 等会,cnblogs不会显示更新时间?我禁赛我自己 UPD:2018.12.15 欢迎大家爆踩标程- painting 我们考虑转化题意 ...

  4. hdu 4442 Physical Examination (2012年金华赛区现场赛A题)

    昨天模拟赛的时候坑了好久,刚开始感觉是dp,仔细一看数据范围太大. 题目大意:一个人要参加考试,一共有n个科目,每个科目都有一个相应的队列,完成这门科目的总时间为a+b*(前面已完成科目所花的总时间) ...

  5. [NOIP2018校模拟赛]T1 阶乘

    题目: 描述 有n个正整数a[i],设它们乘积为p,你可以给p乘上一个正整数q,使p*q刚好为正整数m的阶乘,求m的最小值. 输入 共两行. 第一行一个正整数n. 第二行n个正整数a[i]. 输出 共 ...

  6. Jury Meeting CodeForces - 854D

    Jury Meeting CodeForces - 854D 思路:暴力枚举会议开始的那一天(只需用所有向0点飞的航班的那一天+1去枚举即可),并计算所有人此情况下去0点和从0点出来的最小花费. 具体 ...

  7. No space left on device

    No space left on device 数据库无法启动, 发现是内存没有清空导致. 处理过程: ipcs ipcrm  

  8. 基于ABP的Easyui admin framework正式开放源代码

    下载&反馈:http://www.webplus.org.cn v1.0 (2016/9/21) EF6+MVC5+API2+Easyui1.4.2开发 后台管理不使用iframe,全ajax ...

  9. 安装nginx的一些注意事项

    1.如何彻底屏蔽掉Nginx的banner 为了安全或者某些个人的原因,如果要屏蔽掉nginx的banner,要修改以下几个位置: src/http/ngx_http_header_filter_mo ...

  10. 为sublime Text3 安装插件JS Format

    1. 安装package control 菜单 View - Show Console 或者 ctrl + ~ 快捷键,调出 console.将以下 Python 代码粘贴进去并 enter 执行,不 ...