使用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. 51Nod 1092 回文字符串

    最开始毫无头绪,然后参照了一位dalao的博客,思路是一个正序的字符串将其逆序,然后求最长公共子序列(LCS),emm也属于动态规划. #include <iostream> #inclu ...

  2. C/C++预处理

    C/C++编译系统编译程序的过程为预处理.编译.链接.预处理器是在程序源文件被编译之前根据预处理指令对程序源文件进行处理的程序.预处理器指令以#号开头标识,末尾不包含分号.预处理命令不是C/C++语言 ...

  3. Database UVA - 1592

    对于每组数据,首先通过一个map将每个字符串由一个数字代替,相同的字符串由相同数字代替,不同的字符串由不同数字代替.那么题目就变为了询问是否存在行r1,r2以及列c1,c2使得str[r1][c1]= ...

  4. 自定义view(13)自定义属性

    1.添加attrs.xml文件 在android studio下,在res/values 下新建资源文件attrs.xml 2.添加自定义的属性 在attrs.xml中添加属性,如下.其中format ...

  5. oracle 10g standby 设置

    ##########sample alter system set log_archive_dest_1 = 'LOCATION=USE_DB_RECOVERY_FILE_DEST' scope=bo ...

  6. springboot之项目打包

    通过win中的cmd或者idea中终端,打包并启动项目: 1.mvn package     [打包,在target中生成jar] 2.java -jar xxxxx.jar  [启动jar]

  7. 在Android上使用酷狗歌词API

    参考自http://blog.csdn.net/u010752082/article/details/50810190 代码先贴出来: public void searchLyric(){ final ...

  8. Python3 写入文件

    Demo: file = open("test.txt", "wb")file.write("string") 上面这段代码运行会报类型错误 ...

  9. 复位电路设计——利用PLL锁定信号(lock)产生复位信号

    利用PLL锁定信号(lock)产生复位信号 在FPGA刚上电的时候,系统所需的时钟一般都要经过PLL倍频,在时钟锁定(即稳定输出)以前,整个系统应处于复位状态.因此,我们可以利用PLL的锁定信号来产生 ...

  10. 在DLL中创建窗口时一个值得注意的地方 — UnregisterClass

    背景描述: 今天要测试一份注入代码,拿以前写的创建窗口的DLL来做测试. 第一次注入时一切正常,窗口被成功创建并显示,但在第二次加载时窗口没有显示出来. 经过研究发现在第二次加载DLL时Registe ...