使用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包 使用类来实现代理????请大佬指导,我还没试试,之后会去实验下。


最新文章

  1. Selenium 简单的例子
  2. Freemarker与Servlet
  3. Spring与ActiveMQ整合
  4. WINDOWS HYPER-V加新网卡,设置网络出错
  5. 2015第6周三ztree的使用
  6. Delphi的TService的輸入桌面切換(服务程序)(windows登录界面如何截图)(使用了OpenDesktop和GetThreadDesktop等API)
  7. ERROR security.UserGroupInformation
  8. 上传图片,多图上传,预览功能,js原生无依赖
  9. CentOS升级Python到2.7版本
  10. 【css】主要的块状元素(block element)和内联元素(inline element行内元素)
  11. javac 实现原理
  12. java_25 FileReader类和FileWriter类
  13. C# -- 索引器、枚举类型
  14. vue与jquey
  15. NOIP 提高组 2014 飞扬的小鸟(记录结果再利用的DP)
  16. Java 基础 集合框架
  17. php7新特性总结
  18. String MVC @RequestParam(required=false) int XXX 参数为空报错解决方法
  19. python装饰器,其实就是对闭包的使用。
  20. Dream------scala--开发环境搭建

热门文章

  1. NowCoder数列
  2. 跟我一起玩Win32开发(23):渐变颜色填充
  3. C++中virtual继承的深入理解
  4. nginx添加模块
  5. IE下png图片黑边问题
  6. AtCoder Regular Contest 074 F - Lotus Leaves
  7. 水题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas
  8. h5-24-百度地图-地址解析
  9. iOS中数据类型转换--遇到则记录
  10. Linux下mysql新建账号及权限设置各种方式总结