admin管理员组

文章数量:1530315

现象

运行Spring Web程序时报出错误
BeanNotOfRequiredTypeException: Bean named ‘loginLogService’ is expected to be of type ‘com.qst.service.LoginLogServiceImpl’ but was actually of type ‘com.sun.proxy.$Proxy28’

原因


图中这里直接写的是实现类而不是接口。另一方面
<tx:annotation-driven proxy-target-class=“false” transaction-manager=“transactionManager” />
proxy-target-class=“false” 意味着Spring使用JDK自带的动态代理实现AOP。这样由于JDK动态代理所代理的目标必须是接口,而这里却是一个普通类,所以就抛出此异常。

解决办法

1、 将图中的具体实现类改成接口,这样即使使用JDK动态代理也能实现。
2、proxy-target-class=“true” 使用CGLIG动态代理,这样就可以直接代理普通类。

总结

关于proxy-target-class="true"问题,请参考链接:
Spring: proxy-target-class 决定 用 CGlib 还是 JDK AOP 来生成代理

本文标签: xxxexpectedbeannamedproxy