代理及注入之org.springframework.beans.factory.BeanNotOfRequiredTypeException

错误信息如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminAction': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'adminServiceImpl' must be of type [com.etc.serviceImpl.AdminServiceImpl], but was actually of type [com.sun.proxy.$Proxy46]

Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'adminServiceImpl' must be of type [com.etc.serviceImpl.AdminServiceImpl], but was actually of type [com.sun.proxy.$Proxy46]

代码如下:

@Controller
public class AdminAction {

@Resource(name="adminServiceImpl")
private AdminServiceImpl adminServiceImpl;

}

问题原因:在使用annotation的方式注入spring的bean 时出现,由于spring采用代理的机制导致的

    方案一:由于AdminServiceImpl 是一个实现类,不能使用JDK的动态代理注入,jdk的动态代理不支持类注入,只支持接口方式注入,如果确时不想用spring提供的动态代理,则此时可以选择用cglib代理解决。必须引入cglib的外部包

   方案二:JDK的动态代理需要的是接口方式注入,那就将上面的注入类型修改为接口如下:

@Controller
public class AdminAction {

@Resource(name="adminServiceImpl")
private AdminService adminServiceImpl;

}

在编译问题完美解决



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值