开发Spring过程中几个常见异常(三):java.lang.ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to com.edu.aop.ArithmeticCalculatorImpl at com.edu.aop.Main.main(Main.java:11)

时间:2022-09-22 09:29:56

这个异常是在开发Spring案例时遇到的。

贴一下完整异常信息:

Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to com.edu.aop.ArithmeticCalculatorImpl
at com.edu.aop.Main.main(Main.java:11)

原因:Spring AOP是实现AOP的一种技术,是采用“动态代理技术”实现的。

在该案例中用到了接口,其中小编定义了一个接口ArithmeticCalculator,然后用实体类ArithmeticCalculatorImpl实现了这个接口。

错误代码定位在(此处贴出代码第二句):

ApplicationContext act=new ClassPathXmlApplicationContext("applicationContext.xml");
ArithmeticCalculatorImpl arithmetic=(ArithmeticCalculatorImpl)act.getBean("arithmetic");

再贴一下配置文件中配置信息:

<!-- 配置bean -->
<bean id="arithmetic" class="com.edu.aop.ArithmeticCalculatorImpl"></bean>

可以看到配置的bean是接口的实现类,那么String AOP技术对其进行动态代理,代理的结果对象和这个接口的实现类是同级的。也就是说代理对象和小编定义的接口实现类分别实现了该接口,二者之间根据java语言的转换原则是不能转换的,因此抛出转换异常。

当把转换类型换成接口类型时,就可解决这个异常了。即将红色代码部分改成:

ArithmeticCalculator arithmetic=(ArithmeticCalculator)act.getBean("arithmetic");

参考博客:

http://blog.csdn.net/yinzn2011/article/details/46455973

http://blog.csdn.net/a1491918446/article/details/48715247

开发Spring过程中几个常见异常(三):java.lang.ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to com.edu.aop.ArithmeticCalculatorImpl at com.edu.aop.Main.main(Main.java:11)的更多相关文章

  1. 开发Spring过程中几个常见异常(二):Exception encountered during context initialization - cancelling refresh attempt&colon; org&period;springframework&period;beans&period;factory&period;BeanCreationException&colon; Error creating bean with name &&num;39&semi;a&&num;39&semi; define

    本异常是小编在运行自己另外一篇博文中的例子时遇到的.(附博文:http://www.cnblogs.com/dudududu/p/8482487.html) 完整异常信息: 警告: Exception ...

  2. 开发Spring过程中几个常见异常(一)

    异常一:java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 原因:未导入有关日志管理的jar包或者未添加到路径. ...

  3. java&period;lang&period;ClassCastException&colon; com&period;sun&period;proxy&period;&dollar;Proxy2 cannot be cast to&period;&period;&period;异常

    异常: Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy2 cannot ...

  4. 关于利用动态代理手写数据库连接池的异常 java&period;lang&period;ClassCastException&colon; com&period;sun&period;proxy&period;&dollar;Proxy0 cannot be cast to java&period;sql&period;Connection

    代码如下: final Connection conn=pool.remove(0); //利用动态代理改造close方法 Connection proxy= (Connection) Proxy.n ...

  5. spring 切面织入报错:java&period;lang&period;ClassCastException&colon; com&period;sun&period;proxy&period;&dollar;Proxy7 cannot be cast to&period;&period;&period;

    报这个错,只有一个原因,就是转化的类型不对. 接口过父类的子类,在强制转换的时候,一定要用接口父类来定义. 代码示例: package com.luoluo.dao.impl; import java ...

  6. 【spring】使用spring过程中踩到的坑

    这里简单记录一下,学习spring的时候碰过的异常: 异常:org.springframework.beans.factory.BeanDefinitionStoreException: Unexpe ...

  7. android 开发问题:java&period;lang&period;ClassCastException

    java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl ...

  8. Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求

    Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939   版权声明 ...

  9. 关于火狐浏览器在开发调试过程中,出现javascript&colon;void&lpar;0&rpar;的状态

    关于火狐浏览器在开发调试过程中,出现javascript:void(0)的状态 由于火狐浏览器没有安装 Adobe Flash Player 19 NPAPI这个插件 安装好了之后就可以直接运行了

随机推荐

  1. python 中 五种字典(dict)的遍历方法,实验法比较性能。

    1 .背景: 想知道5种遍历方法,并且知道从性能角度考虑,使用哪种. 2.结论: 使用这种方式: for key,val in AutoDict.iteritems(): temp = "% ...

  2. java按照集合中元素的属性进行排序示例代码

    public class Student { private String name; private int age; private int id; public Student() {  sup ...

  3. API设计和命名

    1.前言 对于前端开发而言,肯定会和API打交道,大家也都会想过怎么设计自己的API.优秀的 API 之于代码,就如良好内涵对于每个人.好的 API 不但利于使用者理解,开发时也会事半功倍,后期维护更 ...

  4. C&num;委托的介绍&lpar;delegate、Action、Func、predicate&rpar;ga

    转载:http://www.cnblogs.com/akwwl/p/3232679.html 感觉写的很好.例子也很简单明了.赞一个 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参 ...

  5. &lbrack;转&rsqb;Apache 配置虚拟主机三种方式

    转自: http://www.cnblogs.com/hi-bazinga/archive/2012/04/23/2466605.html 一.基于IP 1. 假设服务器有个IP地址为192.168. ...

  6. CentOS安装Mysql5&period;6并配置远程连接

    使用的是阿里云提供的CentOS7.3版本,安装的是mysql5.6.40版本.此次安装采用解压压缩包并配置的方式. 1. 卸载自带的Mariadb Centos7将默认数据库mysql替换成了Mar ...

  7. oracle用户 密码永不过期

    ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;

  8. 20145303 实验一 Java开发环境的熟悉&lpar;Linux &plus; Eclipse&rpar;

    20145303 实验一 Java开发环境的熟悉(Linux + Eclipse) 实验题目(4):实现学生成绩管理功能,并进行测试 思路: 对于实现学生成绩管理(student performanc ...

  9. 2018年东北农业大学春季校赛 B wyh的矩阵【找规律】

    链接:https://www.nowcoder.com/acm/contest/93/B来源:牛客网 题目描述 给你一个n*n矩阵,按照顺序填入1到n*n的数,例如n=5,该矩阵如下 1 2 3 4 ...

  10. redis缓冲与数据库

    redis是基于key-value结构存储的,且数据存放在内存中,相对数据库读写较快. 基于redis的优势,将redis中存放用户数据,用户第一次登录时,将用户数据从数据库存放redis中,也可以将 ...