赞
踩
开发中常见这个错误:
The bean 'xxxService' could not be injected as a'com.xxxx.xxx.xxxService' because it is a JDK dynamic proxy that implements:
xxxxxx
Action:
Consider injecting the bean as one of its interfaces orforcing the use of CGLib-based proxiesby setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.
主要配置基于JDK的代理还是基于类的动态代理的配置,这种错误提示需要设置基于类的代理才行。
比如单元测试里注入了实现类,且实现类里有@Transaction注解
如果是springboot项目,在配置里设置
spring.aop.proxy-target-class=true
proxy-target-class属性值决定是基于JDK接口还是基于类的代理被创建。
如果为true代表基于类的代理,
如果为false代表基于JDK接口的代理。
或者在配置类上设置
@EnableAspectJAutoProxy(proxyTargetClass = true)
当然另外的几个注解都支持
@EnableAsync
@EnableCaching
或者
@EnableTransactionManagement (proxyTargetClass = true)
都可以设置proxyTargetClass = true属性
根据自己的情况选择,
@EnableAspectJAutoProxy单纯设置基于类的动态代理,其他的都是开启异步,开启缓存,开启事务管理等注解顺便开启基于类的动态代理。
因为@Autowired默认按类型装配,而 @Resource优先按名称装配,如果使用
- @Resource
- private XabYcdService ycdSerivce;
恰巧有另外一个bean叫“ycdSerivce” 也可能出现这个错误。
比如基于xml的配置
<bean id="xabYcdService" class="com.xxx.xxx.xxx.xxMabYcdService"/>
这里的class并不是实际应该配置的:com.xxx.xxx.xxx.xxXabYcdService
如果觉得本文对你有帮助,欢迎点赞,欢迎关注我,如果有补充欢迎评论交流,我将努力创作更多更好的文章。
--------------------------------------------
我在参见 CSDN 1024 程序员活动(2019年10月24日截止)
如果我的博客对你有帮助,且有时间,欢迎浏览器或者微信扫码,帮我点赞支持我:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。