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 ...
随机推荐
- Rsync 实现远程同步
介绍 rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部 ...
- 在img标签上尽量不要使用onerror事件
在img标签上尽量不要使用onerror事件 因为在之前的时候,我在本地对用户头像修改发现,如果图片加载失败, 使用onerror事件去获取一个默认地址的图片虽然这是可行的,但是如果刚好onerror ...
- mysql用户和授权
CREATE USER 'monitor'@'10.224.32.%' IDENTIFIED BY '123@abAB'; mysql> GRANT select,insert,update O ...
- Minimal string CodeForces - 797C
Minimal string CodeForces - 797C 题意:有一个字符串s和空串t和u,每次操作可以将s的第一个字符取出并删除然后放到t的最后,或者将t的最后一个字符取出并删除然后放到u的 ...
- double发生精度丢失的解决办法
发生精度丢失的原因: 个人理解:机器在运行时,使用2进制形式的计数方式,而我们日常生活中的计算是10进制的,对于整数的加减乘除,double还能适用,但是对于有小数的,则容易发生精度丢失,即用2进制表 ...
- 数据库sql 使用 lag 和OVER 函数和 like 使用 小技巧
1. sample 1: Lag()就是取当前顺序的上一行记录.结合over就是分组统计数据的.Lag()函数,就是去上N行的字段的数据. SQL> select * from x; A---- ...
- hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!
http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE, 更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...
- PoolManager插件(转载)
http://www.xuanyusong.com/archives/2974 前几天我在博客里面分享了为什么Unity实例化很慢的原因,并且也分享了一个缓存池的工具.有朋友给我留言说PoolMana ...
- Xilinx器件原语
原语,其英文名为primitive,是FPGA厂商针对其器件特征开发的一系列常用模块的名称.原语是FPGA芯片中基本元件,代表FPGA中实际拥有的硬件逻辑单元,如LUT,D触发器,RAM等.相当于软件 ...
- Really simple SSH proxy (SOCKS5)
原文: https://thomashunter.name/blog/really-simple-ssh-proxy-socks5/ SOCKS5 is a simple, eloquent meth ...