代理及注入之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;
}
在编译问题完美解决
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;
}
在编译问题完美解决
相关文章推荐
- 运用spring时出现org.springframework.beans.factory.BeanNotOfRequiredTypeException: 错误
- Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'dao' is exp
- 解决: org.springframework.beans.factory.BeanNotOfRequiredTypeException
- 【异常篇-spring】org.springframework.beans.factory.BeanNotOfRequiredTypeException
- org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'faceAction' must be of
- org.springframework.beans.factory.BeanNotOfRequiredTypeException 错误,
- org.springframework.beans.factory.BeanNotOfRequiredTypeException must be of type
- "org.springframework.beans.factory.BeanNotOfRequiredTypeException……" 错误解决方案
- org.springframework.beans.factory.BeanNotOfRequiredTypeException:
- org.springframework.beans.factory.BeanNotOfRequiredTypeException的解决办法
- 异常:org.springframework.beans.factory.BeanNotOfRequiredTypeException
- org.springframework.beans.factory.BeanNotOfRequiredTypeException
- org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named "" must be of type [org.apache.struts.action.Action
- Aurora-hec4.0_scaninvoice_异常_04_org.springframework.beans.factory.BeanNotOfRequiredTypeException
- org.springframework.beans.factory.BeanNotOfRequiredTypeException:
- 在使用Spring AOP 时,启动服务抛出异常: org.springframework.beans.factory.BeanNotOfRequiredTypeException
- org.springframework.beans.factory.BeanNotOfRequiredTypeException
- 处理异常:org.springframework.beans.factory.BeanNotOfRequiredTypeException
- Spring-org.springframework.beans.factory.BeanNotOfRequiredTypeException
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.wei