admin管理员组文章数量:1566616
报错
项目集成redis,在加上@Cacheable报错,加redis做缓存后项目报错
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'UserService' is expected to be of type 'com.cmx.demo.UserService' but was actually of type 'com.sun.proxy.$Proxy84'
at org.springframework.beans.factory.support.DefaultListableBeanFactory.checkBeanNotOfRequiredType(DefaultListableBeanFactory.java:1510)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1489)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:518)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:627)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:318)
... 57 more
原因
Bean named 'UserService' is expected to be of type 'com.cmx.demo.UserService' but was actually of type 'com.sun.proxy.$Proxy84'
从报错可以看出 ‘com.sun.proxy.$Proxy84’ 不是我们期待的bean ‘UserService’
原因是我们加了@Cacheable注解,该注解将会在方法前进行拦截,验证当前参数是否存在缓存,如果不存在在进行方法执行,最后在返回后进行将key value缓存。这个拦截使用到了AOP的思想,面向切面编程,有JDK代理和Cglib代理2种,但是底层默认是JDK代理。但是JDK代理有个致命的缺点,就是只能代理有实现过接口的类,而Cglib则都可以代理
当前项目起不来的原因正是我们的UserService
没有实现接口,正常情况下应该是UserService
实现IUserService
解决方案
- 将每个Service或者需要使用到代理的类都实现接口,但是被增强的也只能是接口中的方法
- 强制使用Cglib代理
在spring.xml中开启强制cglib代理,并且新增2个jar包(aspectjrt、aspectjweaver)
<aop:aspectj-autoproxy proxy-target-class="true"/>
本文标签: 报错typeBeanNotOfRequiredTypeExceptionSpringquot
版权声明:本文标题:Spring报错:BeanNotOfRequiredTypeException but was actually of type "com.sun.proxy.$Proxy" 开启cglib代理 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1726602181a1077163.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论