现象

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的更多相关文章

  1. SpringBoot @AutoWired Null

    在调用工具类时,若工具类中含有@Autowired注解,这此工具类对象必须同样使用@Autowired注解,否则工具类中的Spring注入的对象都为空值,这里的HadoopTest就是这样 比如MyC ...

  2. Springboot @Autowired 无法注入问题

    特别提醒:一定要注意文件结构 WebappApplication 一定要在包的最外层,否则Spring无法对所有的类进行托管,会造成@Autowired 无法注入. 1. 添加工具类获取在 Sprin ...

  3. SpringBoot @Autowired中注入静态方法或者静态变量

    注:用static去定义一个注入的方法或者配置文件值变量,编译时不会有任何异常,运行时会报空指针. Spring官方不推荐此种方法. 原理: https://www.cnblogs.com/chenf ...

  4. Spring Autowired错误???

    @SpringBootApplicationpublic class TestMqApplication extends SpringBootServletInitializer { @Suppres ...

  5. Spring IOC的核心机制:实例化与注入

    上文我们介绍了IOC和DI,IOC是一种设计模式,DI是它的具体实现,有很多的框架都有这样的实现,本文主要以spring框架的实现,来看具体的注入实现逻辑. spring是如何将对象加入容器的 spr ...

  6. 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法

    最近使用SpringBoot的自定义拦截器,在拦截器中注入了一个DAO,准备下面作相应操作,拦截器代码: public class TokenInterceptor implements Handle ...

  7. springboot使用jpa+mongodb时,xxxRepository不能Autowired的问题

    springboot启动类: @SpringBootApplication public class MainApp { public static void main(String[] args) ...

  8. 解决SpringBoot的@Autowired无法注入问题

    问题:@Autowired无法自动注入 思路:SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描!"Application类"是指 ...

  9. Springboot中如何在Utils类中使用@Autowired注入bean

    Springboot中如果希望在Utils工具类中,使用到我们已经定义过的Dao层或者Service层Bean,可以如下编写Utils类: 1. 使用@Component注解标记工具类Statisti ...

随机推荐

  1. mysql 案例 ~ pt-xtrabackup 使用

    一 简介:学习innobackup工具使用 二 功能: 备份全库/单库/单表 三 常用命令  一 全库  目的 每天日常备份  备份 innobackupex --defaults-file=/etc ...

  2. python - 系统交互操作(subprocess)

    本文摘于云游道士 链接:https://www.cnblogs.com/yyds/p/7288916.html 个人简化,便于查询. 命令行指令的执行通常有两个比较关注的结果: 命令执行的状态码--表 ...

  3. .Net Core中使用RabbitMQ

    (1).引入依赖 RabbitMQ.Client (2).编写发布者代码 var connectionFactory = new ConnectionFactory() { HostName=&quo ...

  4. ftpdata目录下日期目录权限问题

    由于APP Server由root用户启动,创建目录默认为root:root用户:用户组权限 需chown -R wingupload ftpdata执行后,WING才能上传成功 或者写成脚本,每天凌 ...

  5. 用rand()和srand()产生伪随机数的方法总结 【转】

    转自:http://blog.chinaunix.net/uid-26722078-id-3754502.html 标准库(被包含于中)提供两个帮助生成伪随机数的函数: 函数一:int rand(vo ...

  6. Raw Socket vs Stream Socket vs datagram socket,原始套接字与流式套接字与数据报套接字

    https://opensourceforu.com/2015/03/a-guide-to-using-raw-sockets/ In this tutorial, let’s take a look ...

  7. 『实践』Matlab实现Flyod求最短距离及存储最优路径

    Matlab实现Flyod求最短距离及存储最优路径 一.实际数据 已知图中所有节点的X.Y坐标. 图中的节点编号:矩阵中的编号 J01-J62:1-62; F01-F60:63-122; Z01-Z0 ...

  8. Docker+STF在ubuntu下测试环境搭建(详细搭建步骤及踩坑记录)

    一.什么是OpenSTF? STF又称OpenSTF,它是一个手机设备管理平台,可以对手机进行远程管理.调试.远程手机桌面监控等操作.这个系统类似于目前很流行的云测服务比如Testin,虽然网页上提供 ...

  9. Project Euler Problem6

    Sum square difference Problem 6 The sum of the squares of the first ten natural numbers is, 12 + 22  ...

  10. [转]MySQL中函数CONCAT及GROUP_CONCAT

    一.CONCAT()函数 CONCAT()函数用于将多个字符串连接成一个字符串. 使用数据表Info作为示例,其中SELECT id,name FROM info LIMIT 1;的返回结果为 +-- ...