Spring异常——BeanNotOfRequiredTypeException
使用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的更多相关文章
- Spring异常抛出触发事务回滚
Spring.EJB的声明式事务默认情况下都是在抛出unchecked exception后才会触发事务的回滚 /** * 如果在spring事务配置中不为切入点(如这里的切入点可以定义成test*) ...
- (转)spring异常抛出触发事务回滚策略
背景:在面试时候问到事务方法在调用过程中出现异常,是否会传递的问题,平时接触的比较少,有些懵逼. spring异常抛出触发事务回滚策略 Spring.EJB的声明式事务默认情况下都是在抛出unchec ...
- spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet
spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet 情况 ...
- Hibernate整合Spring异常'sessionFactory' or 'hibernateTemplate' is required
今日在写GenericDao时,发现了一个异常,内容如下: org.springframework.beans.factory.BeanCreationException: Error creatin ...
- spring 异常记录
1.异常: java.lang.IllegalArgumentException: No converter found for return value of type: class java.ut ...
- Spring异常累计(1)Spring注解与扫描,NoUniqueBeanDefinitionException
spring中可以使用注解机制,代替传统的在xml中配置一个bean. 如 <pre name="code" class="java">@Compo ...
- spring 异常管理机制
三.异常处理的几种实现: 3.1.在经典的三层架构模型中,通常都是这样来进行异常处理的: A.持久层一般抛出的是RuntiomeException类型的异常,一般不处理,直接向上抛出. B.业务层一般 ...
- spring异常处理器
一.本篇文章旨在讨论异常处理器: 1.因为异常处理器在实战中通常用来处理开发人员自定义的运行时异常,所以如果要了解如何自定义运行时异常,请自行搜索相关资料. 2.本文的demo用IndexOutOfB ...
- spring异常
1.The type org.springframework.core.NestedRuntimeException cannot be resolved. It is indirectly refe ...
随机推荐
- Appium问题记录
1.Appium 提示覆盖安装Appium Android Input Manager for Unicode 问题 安卓手机在新版本中Appium 总是提示覆盖安装Appium Android In ...
- 1-10super和this关键字
什么是super? super代表的是当前子类对象中的父类型特征. 什么时候使用super? 子类和父类中都有某个数据,例如,子类和父类中都有name这个属性.如果要再子类中访问父类中的name属性, ...
- PopupWindow(3)back,home 键无法关闭popupwindow的解决方案
private PopupWindow mPopupWindow; //popup window 一般popuowindow 要都个显示view,本例子中view模拟菜单. private View ...
- 转 OGG Troubleshooting-Database error 1 (ORA-00001: unique constraint ...)
Q5: After imp data to target, when we start replc process, we find the following error: 2011-11-10 0 ...
- MongoDB管理练习
一.索引 1.插入10W条数据 文档内容为:{name:zs-i,age:1} 2016-06-07T14:35:57.041+0800 I CONTROL [initandlisten] > ...
- JVM 内存机制理解【转自http://www.cnblogs.com/dingyingsi/p/3760447.html】
我们知道,计算机CPU和内存的交互是最频繁的,内存是我们的高速缓存区,用户磁盘和CPU的交互,而CPU运转速度越来越快,磁盘远远跟不上CPU的读写速度,才设计了内存,用户缓冲用户IO等待导致CPU的等 ...
- PoolManager插件(转载)
http://www.xuanyusong.com/archives/2974 前几天我在博客里面分享了为什么Unity实例化很慢的原因,并且也分享了一个缓存池的工具.有朋友给我留言说PoolMana ...
- 用NPOI从DataTable到Excel,向Excel模板填充数据
DataTable---->Excel,填充数据 private IWorkbook workbook = null; private ISheet sheet = null; private ...
- hihocoder offer收割编程练习赛8 A 小Ho的强迫症
思路: 乱搞. 实现: #include <iostream> #include <cstdio> using namespace std; typedef long long ...
- 一条陌生的出路【过往d心声】
一条陌生的出路 Vashon的心声 人生就像一列车,车上总有形形色色的人穿梭往来.你也可能会在车上遇到很多你以为有缘分的人,但是车也会有停下来的时候,总会有人从人生这列车上上下下,当你下去的时候你挥挥 ...