您的位置:首页 > 编程语言 > Java开发

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

2017-04-17 19:27 721 查看
错误信息如下:

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;

}

在编译问题完美解决
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐