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); } }
springboot Autowired BeanNotOfRequiredTypeException的更多相关文章
- SpringBoot @AutoWired Null
在调用工具类时,若工具类中含有@Autowired注解,这此工具类对象必须同样使用@Autowired注解,否则工具类中的Spring注入的对象都为空值,这里的HadoopTest就是这样 比如MyC ...
- Springboot @Autowired 无法注入问题
特别提醒:一定要注意文件结构 WebappApplication 一定要在包的最外层,否则Spring无法对所有的类进行托管,会造成@Autowired 无法注入. 1. 添加工具类获取在 Sprin ...
- SpringBoot @Autowired中注入静态方法或者静态变量
注:用static去定义一个注入的方法或者配置文件值变量,编译时不会有任何异常,运行时会报空指针. Spring官方不推荐此种方法. 原理: https://www.cnblogs.com/chenf ...
- Spring Autowired错误???
@SpringBootApplicationpublic class TestMqApplication extends SpringBootServletInitializer { @Suppres ...
- Spring IOC的核心机制:实例化与注入
上文我们介绍了IOC和DI,IOC是一种设计模式,DI是它的具体实现,有很多的框架都有这样的实现,本文主要以spring框架的实现,来看具体的注入实现逻辑. spring是如何将对象加入容器的 spr ...
- 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
最近使用SpringBoot的自定义拦截器,在拦截器中注入了一个DAO,准备下面作相应操作,拦截器代码: public class TokenInterceptor implements Handle ...
- springboot使用jpa+mongodb时,xxxRepository不能Autowired的问题
springboot启动类: @SpringBootApplication public class MainApp { public static void main(String[] args) ...
- 解决SpringBoot的@Autowired无法注入问题
问题:@Autowired无法自动注入 思路:SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描!"Application类"是指 ...
- Springboot中如何在Utils类中使用@Autowired注入bean
Springboot中如果希望在Utils工具类中,使用到我们已经定义过的Dao层或者Service层Bean,可以如下编写Utils类: 1. 使用@Component注解标记工具类Statisti ...
随机推荐
- include的作用
#include发生在预处理阶段,整个编译链接过程,#include是最简单的了,没有之一.就是在include的位置直接把文件原原本本完完整整一字不落的包含进来,下面举一个极端点的例子: //fil ...
- Android中的Service:Binder,Messenger,AIDL
http://blog.csdn.net/luoyanglizi/article/details/51594016 http://blog.csdn.net/luoyanglizi/article/d ...
- ROS中的CMakeLists.txt
在ROS的编程过程中,如果CMakeLists.txt如果写不好,编译就很难成功.如果看不懂CMakeLists.txt那么很多错误你也不知道时什么回事.所以深入了解它是很有必要的.现在我们就来看看它 ...
- hadoop学习笔记之一步一步部署hadoop分布式集群
一.准备工作 同一个局域网中的三台linux虚拟机,我用的是redhat6.4,如果主机是windows操作系统,可以先安装vmware workstation, 然后在workstation中装上3 ...
- python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib
python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib ...
- windows命令行中英文切换
Windows下cmd命令提示符窗口的语言设置(中英) 打开cmd命令提示窗口 输入 chcp 936 使用ping 命令 显示中文 2 同样 输入chcp 437 3 使用ping 命令
- 本体【Ontology】综述
原文地址:http://blog.csdn.net/moonsheep_liu/article/details/22329873 本体作为一种能在语义和知识层次上描述领域概念的建模工具,其目标是捕获相 ...
- centos系统中perl进程病毒占用大量网络流量导致网络瘫痪的问题分析及解决方案
故障现象: 1.系统在早上9点的时候非常慢,单台服务器占用流量很大,使交换机流量被占满,而连累挂在同一交换机上的其他应用也无法提供服务,或者速度非常慢 2.通过查看进程发现大量的perl程序占 ...
- spring boot 中的热部署
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>sprin ...
- Android学习笔记————利用JDBC连接服务器数据库
/******************************************************************************************** * auth ...