使用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. (转) ACM必备(学完一个就加亮一个)不多,就这些!

    时间复杂度(渐近时间复杂度的严格定义,NP问题,时间复杂度的分析方法,主定理)排序算法(平方排序算法的应用,Shell排序,快速排序,归并排序,时间复杂度下界,三种线性时间排  序,外部排序)数论(整 ...

  2. 递推DP URAL 1031 Railway Tickets

    题目传送门 /* 简单递推DP:读题烦!在区间内的都更新一遍,dp[]初始化INF 注意:s1与s2大小不一定,坑! 详细解释:http://blog.csdn.net/kk303/article/d ...

  3. 1-2JDK的安装和环境变量设置

    JDK的下载和安装 可以去oracle官网里面下载JDK:http://www.oracle.com 英文不好的同学可以通过下面这个百度网盘的链接进行下载:http://pan.baidu.com/s ...

  4. java常用类要点总结

  5. c库函数-字符串

    一 strok:从字符串中按照分隔符提取所有字串 char s[] = "水发产品,47.6,不合格,mg/kg,17-05-21 15:04;";  char *delim = ...

  6. [转]合理使用ArrayMap代替HashMap

    合理使用ArrayMap代替HashMap 2016年07月08日 15:34:44 阅读数:5938 转载请标注: 披萨大叔的博客 http://blog.csdn.net/qq_27258799/ ...

  7. html制作简单框架网页二 实现自己的影音驿站 操作步骤及源文件下载 (可播放mp4、avi、mpg、asx、swf各种文件的视频播放代码)

    新增视频播放功能如下图: 左侧网页left.html代码如下: <meta charset="utf-8"> <body style="backgrou ...

  8. 最新版kubernetesV1.14.1集群一键自动部署脚本

    部署命令如下:详情及注意事项请看README.md git clone https://github.com/luckman666/deploy_Kubernetes-v1.14.1.git cd d ...

  9. Java进化的尽头

    转载需声明:原文链接网址:http://www.artima.com/weblogs/viewpost.jsp?thread=221903 Java: Evolutionary Dead End 我在 ...

  10. 做OJ项目时遇到的坑

    1.js代码写在Dom加载前,导致highcharts在ie8能够显示,而ie高版本和其他浏览器不能显示 我的理解:由于IE8和其他浏览器的js解析机制不同,ie8是在等dom全部加载完才开始执行js ...