Bean named 'XXX' must be of type [XXX], but was actually of type [com.sun.proxy.$Proxy34]

编程入门 行业动态 更新时间:2024-10-26 07:30:38

今天遇到一个问题,在修改以往项目的bug进行修复的时候,加上了事务管理,结果项目在启动的时候报错:

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'pushController': Injection of resource dependencies failed; 
nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'deviceTokenService' must be of type [com.paralworld.app.service.impl.DeviceTokenServiceImpl], but was actually of type [com.sun.proxy.$Proxy34]	
Bean named 'deviceTokenService' must be of type [com.paralworld.app.service.impl.DeviceTokenServiceI	

根据报错信息,原来是早controller中注入service的时候,使用了service的实现类,而不是接口类。

本来这样注入的方式,之前也是可以正常运行的,但是在service的实现类上加上了事务管理后,就出问题了:


在spring中@Transaction是通过AOP实现的,而spring对AOP有两种实现方式,一种是动态代理,它是通过接口方式实现的,要求所代理的类一定是实现了某一个接口,对一般的类就无法代理,spring默认是这种;通过设置proxy-target-class="true",则是使用CGLIB实现AOP,CGLIB直接生成二进制码,使得普通类也可以实现AOP。在没有设置proxy-target-class="true"时,使用动态代理,是一个临时生成的类,如proxy17,它不是@Resource指定的类,因此出现了上述错误。

更多推荐

Bean named 'XXX' must be of type [XXX], but was actually of type [com.

本文发布于:2023-06-13 16:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385365.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:XXX   named   Bean   type   proxy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!