现象

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. DSO windowed optimization 代码 (1)

    这里不想解释怎么 marginalize,什么是 First-Estimates Jacobian (FEJ).这里只看看代码,看看Hessian矩阵是怎么构造出来的. 1 优化流程 整个优化过程,也 ...

  2. 【vue报错】——listen EADDRINUSE :::8080 解决方案

    题原因: 此项错误表示 8080 端口被占用 解决方案一: 打开cmd输入:netstat -ano|findstr "8080"查看所有端口信息,并通过findstr “8080 ...

  3. Java读取txt文件——(二)

    Txt数据

  4. Python中的__init__()和__call__()函数

    Python中的__init__()和__call__()函数 在Python的class中有一些函数往往具有特殊的意义.__init__()和__call__()就是class很有用的两类特殊的函数 ...

  5. k-means 图像分割

    经典的无监督聚类算法,不多说,上代码. import numpy as np import pandas as pd import copy import matplotlib.pyplot as p ...

  6. zabbix3.0.4安装grapha实现多台主机相同监控项集中展示

    zabbix3.0.4安装grapha图形展示系统 操作系统 # cat /etc/redhat-release  CentOS Linux release 7.2.1511 (Core) 1.安装g ...

  7. 在Mac上安装GTK(go语言GUI)

    1.在终端输入:xcode-select --install 安装command line工具, 如果安装了Xcode, 就直接跳过该步骤 2. 在终端输入:ruby -e "$(curl ...

  8. es6 entries(),keys() 和 values()

    for (let index of ['a', 'b'].keys()) { console.log(index); } // 0 for (let elem of ['a', 'b'].values ...

  9. Route pattern cannot reference variable name more than once

    在用 Laravel Backpack 写一个定制化的 CRUD 页面.例如,一个指定店铺所拥有的商品的 CRUD 页面. 起初路由我是这样写的 CRUD::resource('products-of ...

  10. hdu5493 树状数组+二分

    数字的字典序,,有点迷,网上看题解也没有明说,总之越大的数字放在后面就行了 利用二分找到前k个空位即可 /* 每个人有一个独特的高度,第i个人高hi,前面有ki个人比他高或后面有ki个人比他高 请求出 ...