springboot Autowired BeanNotOfRequiredTypeException
现象
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'xxxxImpl' is expected to be of type 'com.xxx.xxxImpl' but was actually of type 'com.sun.proxy.$Proxy62'
直接Autowired一个实现类,而不是接口
@Autowired private XxxServiceImpl xxxService;
解决方案
1. Autowired接口
2. 使用EnableAspectJAutoProxy
SpringBootApplication @EnableAspectJAutoProxy(proxyTargetClass = true) public class Application {public static void main(String[] args) {SpringApplication app = new SpringApplication(Application.class);app.run(args);} }
设置proxy-target-class为true即使用cglib的方式代理对象,默认是jdk方式代理。
jdk的动态代理不支持类注入,只支持接口方式注入。
动态代理类型判断
//org.springframework.aop.framework.DefaultAopProxyFactory //参数AdvisedSupport 是Spring AOP配置相关类 public AopProxy createAopProxy(AdvisedSupport advisedSupport) throws AopConfigException { //在此判断使用JDK动态代理还是CGLIB代理 if (advisedSupport.isOptimize() || advisedSupport.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(advisedSupport)) { if (!cglibAvailable) { throw new AopConfigException( "Cannot proxy target class because CGLIB2 is not available. " + "Add CGLIB to the class path or specify proxy interfaces."); } return CglibProxyFactory.createCglibProxy(advisedSupport); } else { return new JdkDynamicAopProxy(advisedSupport); } }
转载于:https://www.cnblogs.com/hujunzheng/p/8428422.html
springboot Autowired BeanNotOfRequiredTypeException相关推荐
- 一次SpringBoot AutoWired 注入服务为null的事件
问题表述: java.lang.NullPointerExceptionat com.ynd.back.socket.WebSocket.onMessage(WebSocket.java:113)at ...
- 2021年22月3日
springboot @Autowired注解 错误变为警告 Mybatis注解 <dependency><groupId>junit</groupId><a ...
- SpringBoot集成Quartz(解决@Autowired空指针Null问题即依赖注入的属性为null)
SpringBoot集成Quartz(解决@Autowired空指针Null问题即依赖注入的属性为null) 参考文章: (1)SpringBoot集成Quartz(解决@Autowired空指针Nu ...
- 关于springboot工具类中@Autowired注入bean,用static直接修饰,静态方法使用bean时报空指针异常错误...
错误场景: springboot + mybatis 在工具类的静态方法中,需要使用mapper(其他bean也一样),所以最开始直接使用@Autowired进行了注入,代码如下: @Autowire ...
- autowired 静态方法使用_关于springboot工具类中@Autowired注入bean,用static直接修饰,静态方法使用bean时报空指针异常错误...
错误场景: springboot + mybatis 在工具类的静态方法中,需要使用mapper(其他bean也一样),所以最开始直接使用@Autowired进行了注入,代码如下: @Autowire ...
- spring springboot websocket 不能注入( @Autowired ) service bean 报 null 错误
spring 或 springboot 的 websocket 里面使用 @Autowired 注入 service 或 bean 时,报空指针异常,service 为 null(并不是不能被注入). ...
- SpringBoot 中@Autowired 注入失效原因及解决方法
SpringBoot 中@Autowired 注入失效原因及解决方法 1.原因分析 1.1 包没有被扫描到通过@Autowired注入的类所在的包路径不在Application启动类所在的包/子包路径 ...
- SpringBoot学习——@Autowired自动注入报:could not be found问题的理解和解决方案
微服务应用程序中,我们会通过Java后台的方式发送http请求并调用其他注册在Spring Cloud Eureka server上的微服务,之前我们可能会手动封装一个Http发送请求类,然后通过其中 ...
- 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
[SpringBoot]拦截器使用@Autowired注入接口为null解决方法 参考文章: (1)[SpringBoot]拦截器使用@Autowired注入接口为null解决方法 (2)https: ...
最新文章
- 最牛逼的阿里巴巴内部Java调优方案,没有之一!
- 第十六届智能车竞赛线上全国总决赛远程组委会监督腾讯会议
- linux 打包压缩工具
- try catch 自定义捕获异常
- 解决cmd命令查看python版本“python不是内部命令或外部命令,也不是可执行程序解决方案”的问题
- Spring Security OAuth2 Demo -- good
- 运行caffe自带的mnist实例教程
- Codeforces Round #358 (Div. 2) A. Alyona and Numbers 水题
- LeetCode 1935. 可以输入的最大单词数
- 深入浅出 MySQL 优先队列
- SpringMVC+MyBatis+MySQL实现分页
- 为什么i3的cpu基础频率最高,达到4.0了?
- 目前人工智能的主要研究方向是哪些?
- python flask 路由_python框架flask入门之路由及简单实现方法
- SwiftUI Button精品组件之完美的SwiftUI抖动按钮效果(教程含源码)
- php 使用gd库,PHP的gd库使用基础教程
- http、https、tcp三次握手,四次挥手学习笔记
- 以太坊合并后下跌22%,我们分析链上的数据看看是否能找到答
- 不平凡的2021,末流普本生秋招上岸大厂的历程
- 《一》注册订阅号并完成基本配置
热门文章
- 设置android应用闪屏图片_Android实现启动页面(闪屏页面)
- mysql 5.1 db2i_DB2 9.5.0.0升级至9.5.0.9(小版本升级)
- uni-app 微信小程序授权登录
- vue中设置height:100%无效的问题及解决方法
- Java裁剪压缩PNG图片,透明背景色变黑问题解决
- linux搭建SonarQube_Oracle
- 前端学习笔记之DOM(一)
- 读取扫码枪输入_为何超市“扫码枪”这么强?不输密码钱就没了,现在知道还不晚...
- python删除链表的倒数第k个节点,剑指offer 链表中的倒数第K个节点 Python and C++
- 多机器人路径规划的代码_知荐 | 地平线机器人算法工程师总结六大路径规划算法...