admin管理员组文章数量:1565768
在SSM项目中的Service层遇到了这个报错
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean
named ‘employeeServiceImpl’ is expected to be of type
‘com.mlj.service.EmployeeServiceImpl’ but was actually of type
‘com.sun.proxy.$Proxy19’
名为employeeServiceImpl的bean类型本应该是com.mlj.service.EmployeeServiceImpl
实际上却是com.sun.proxy.$Proxy19
这个问题是AOP引起的
SpringAOP实现的两种方式动态代理: JDK和Cglib
JDK:目标对象必须是实现某一接口
Cglib:不需要接口,但需要两个jar包:asm.jar和cglib.jar
报错代码:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
EmployeeServiceImpl employeeServiceImpl = applicationContext.getBean("employeeServiceImpl", EmployeeServiceImpl.class);
employeeServiceImpl.getAllEmp();
问题出在这里的employeeServiceImpl 必须向上转型为接口,不能用接口的实现类(EmployeeServiceImpl)来转换Proxy的实现类,它们是同级,应该用共同的接口(EmployeeService)来转换
把代码改为
EmployeeService employeeServiceImpl =applicationContext.getBean(“employeeServiceImpl”,EmployeeService.class);
简单地说就是要用该类的接口来转换,而且必须是该类的接口
本文标签: xxxexpectedbeannamedtype
版权声明:本文标题:Bean named ‘xxx‘ is expected to be of type ‘xxxx‘ but was actually of type ‘com.sun..proxy.$Proxy19‘ 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1726602442a1077192.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论