现象

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. Linux常用命令(一)查看日志

    当日志文件存储很大时,需要Linux命令查看: Log 在目录 /var/log/   下 常用命令: tail head grep sed cat tac https://blog.csdn.net ...

  2. mysql 案例 ~ pt-io工具的使用

    一 简介:如何使用pt-iopfile调查io具体信息二 目的:利用pt-iopfile分析mysql内部IO操作密集的文件,用以发现问题三 使用: pt-iopfile -p mysql_pid   ...

  3. JobService相关

    1.在构建JobInfo时,如果设置setPersisted(true),则需要应用申请RECEIVE_BOOT_COMPLETED权限. 这个权限只需要在应用首次安装时被授予,后面覆盖安装时便会默认 ...

  4. eclips环境下开发spring boot项目,application.properties配置文件下中文乱码解决方案

    如以上,application.properties文件下中文乱码.发生乱码一般都是由于编码格式不一样导致的. 打开Window-Preferences-General-content Types-T ...

  5. python2和3使用pip时的问题

    win10,电脑之前装有Anaconda,python2.因为需要用到python3,所以直接下载安装了python3.python3默认路径在c盘.我将其移到D盘并修改了两个环境变量.这时电脑的默认 ...

  6. centos6.5下系统编译定制iptables防火墙扩展layer7应用层访问控制功能及应用限制QQ2016上网

    iptables防火墙扩展之layer7应用层访问控制 概述: iptables防火墙是工作在网络层,针对TCP/IP数据包实施过滤和限制,属于典型的包过滤防火墙.以基于网络层的数据包过滤机制为主,同 ...

  7. GBDT学习

    白话GBDT: https://blog.csdn.net/qq_26598445/article/details/80853873 优点: 预测精度高 适合低维数据 能处理非线性数据,该版本GBDT ...

  8. PYTHON-函数的定义与调用,返回值,和参数

    函数基础'''1. 什么是函数 具备某一功能的工具->函数 事先准备工具的过程--->函数的定义 遇到应用场景,拿来就用---->函数的调用 函数分类两大类: 1. 内置函数 2. ...

  9. redhat5 设置静态ip

    Last login: Sat Oct 14 16:19:13 2017 # 进入ip凭证文件设置地方 [root@oracle ~]# cd /etc/sysconfig/network-scrip ...

  10. java 语言的主要特点

    java 语言主要特点如下: 1:简单 2:面向对象 3:分布性 4:可移植性 5:安全性 6:健壮性 二:java 主要术语 三:java 核心是面向对象程序设计OOP 四:封装 五:多态 六:继承 ...