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]

测试类代码:

 1 package com.htd.test;
 2 
 3 import javax.annotation.Resource;
 4 import org.junit.Test;
 5 import org.junit.runner.RunWith;
 6 import org.springframework.test.context.ContextConfiguration;
 7 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 8 import com.htd.domain.Customer;
 9 import com.htd.service.impl.RegisterServiceImpl;
10 
11 @RunWith(SpringJUnit4ClassRunner.class)
12 @ContextConfiguration("classpath:applicationContext.xml")
13 public class Test1 {
14     @Resource(name="registerService")
15     private RegisterServiceImpl registerService;
16     
17     @Test
18     public void test1() {
19         Customer customer=new Customer();
20         customer.setCus_name("eastry");
21         customer.setCus_email("123456@qq.com");
22         customer.setCus_pwd("123");
23         customer.setCus_id(1);
24         registerService.save(customer);
25     }
26     
27 }
View Code

RegisterServiceImpl类:

 1 package com.htd.service.impl;
 2 
 3 import org.springframework.transaction.annotation.Transactional;
 4 
 5 import com.htd.dao.impl.CustomerDaoImpl;
 6 import com.htd.domain.Customer;
 7 import com.htd.service.IRegisterService;
 8 
 9 @Transactional
10 public class RegisterServiceImpl implements IRegisterService {
11     //xml方式注入
12     private CustomerDaoImpl customerDao;
13     public void setCustomerDao(CustomerDaoImpl customerDao) {
14         this.customerDao = customerDao;
15     }
16     @Override
17     public void save(Customer customer) {
18         customerDao.save(customer);
19     }
20 }
View Code

RegisterServiceImpl的接口IRegisterService:

1 import com.htd.domain.Customer;
2 
3 public interface IRegisterService {
4     public void save(Customer customer);
5 }
View Code

运行结果:

不好,红了。。。。。


 这里没有使用Cglib代理,Java的动态代理是使用接口实现的,但是在Test类里面注入的时候是使用Service的实现类(即:RegisterServiceImpl)注入的,所以这里会出错。

如图:

 修改成下列代码测试成功:

修改后的测试类代码:

 1 import javax.annotation.Resource;
 2 
 3 import org.junit.Test;
 4 import org.junit.runner.RunWith;
 5 import org.springframework.test.context.ContextConfiguration;
 6 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 7 
 8 import com.htd.domain.Customer;
 9 import com.htd.service.IRegisterService;
10 @RunWith(SpringJUnit4ClassRunner.class)
11 @ContextConfiguration("classpath:applicationContext.xml")
12 public class Test1 {
13     @Resource(name="registerService")
14     private IRegisterService registerService;
15     
16     @Test
17     public void test1() {
18         Customer customer=new Customer();
19         customer.setCus_name("eastry");
20         customer.setCus_email("123456@qq.com");
21         customer.setCus_pwd("123");
22         customer.setCus_id(1);
23         registerService.save(customer);
24     }
25 }
View Code

 运行结果:

嗯,变绿了,很好。。。。。。。。。。。

 


 

如果想用类注入,就要导入Cglib的jar包 使用类来实现代理????请大佬指导,我还没试试,之后会去实验下。

 


 

转载于:https://www.cnblogs.com/Eastry/p/10466172.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值