Bean named ‘XXX’ is expected to be of type ‘com.baven.service.XXX’ but was actually of type ‘com.sun.proxy.$Proxy7’
名为“ XXX”的Bean的类型应为“ com.baven.service.XXX”,但实际上其类型为“ com.sun.proxy.$ Proxy7”
像这类问题呢,是在我复习Spring aop的时候遇到的。翻了下资料发现才想起来。
Spring使用的动态代理有两种:
- JDK 的 Proxy : 要实现至少一个接口才能对方法拦截
- CGLIB:使用需要导包cglib.jar、asm.jar
我的问题就出在了第一个,以下为例
StudentServiceImpl service = context.getBean("studentService", StudentServiceImpl.class);
注:不能用接口的实现类StudentServiceImpl
,而因该用共同的接口StudentService
。
StudentService service = context.getBean("studentService", StudentService.class);
重启测试: 问题解决
愿世上没有Bug