but was actually of type 'com.su
2021-02-07 本文已影响0人
帅气十里不如你
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'accountService' is expected to be of type 'com.marchsoft.service.impl.AccountServiceImpl' but was actually of type 'com.sun.proxy.$Proxy8'
-
分析产生的原因:
- 我百度了一下产生这种错误的原因是Spring aop代理混用的问题
- 在java中默认使用的动态代理是JDK proxy基于接口的代理, 如果你报错上述信息,极有可能是你注入的时候,注入到了类上面,
- **有两种解决方案: **
- 在xml文件中配置如下代码:
<aop:aspectj-autoproxy proxy-target-class="true"/>这句代码的意思就是:开启Spring注解配置
在<aop:config>这个标签的下面写 - 就是将注入注入到接口上面,如下所示
IAccountService as = ac.getBean("accountService", AccountServiceImpl.class); //其实刚刚把上面这句代码改为下面这句就可以了 IAccountService as = ac.getBean("accountService", IAccountService.class); //就是把类改为接口
- 在xml文件中配置如下代码: