Spring异常——BeanNotOfRequiredTypeException
2024-08-29 22:08:35
使用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包 使用类来实现代理????请大佬指导,我还没试试,之后会去实验下。
最新文章
- Selenium 简单的例子
- Freemarker与Servlet
- Spring与ActiveMQ整合
- WINDOWS HYPER-V加新网卡,设置网络出错
- 2015第6周三ztree的使用
- Delphi的TService的輸入桌面切換(服务程序)(windows登录界面如何截图)(使用了OpenDesktop和GetThreadDesktop等API)
- ERROR security.UserGroupInformation
- 上传图片,多图上传,预览功能,js原生无依赖
- CentOS升级Python到2.7版本
- 【css】主要的块状元素(block element)和内联元素(inline element行内元素)
- javac 实现原理
- java_25 FileReader类和FileWriter类
- C# -- 索引器、枚举类型
- vue与jquey
- NOIP 提高组 2014 飞扬的小鸟(记录结果再利用的DP)
- Java 基础 集合框架
- php7新特性总结
- String MVC @RequestParam(required=false) int XXX 参数为空报错解决方法
- python装饰器,其实就是对闭包的使用。
- Dream------scala--开发环境搭建