开发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)
这个异常是在开发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)的更多相关文章
- 开发Spring过程中几个常见异常(二):Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'a' define
本异常是小编在运行自己另外一篇博文中的例子时遇到的.(附博文:http://www.cnblogs.com/dudududu/p/8482487.html) 完整异常信息: 警告: Exception ...
- 开发Spring过程中几个常见异常(一)
异常一:java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 原因:未导入有关日志管理的jar包或者未添加到路径. ...
- java.lang.ClassCastException: com.sun.proxy.$Proxy2 cannot be cast to...异常
异常: Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy2 cannot ...
- 关于利用动态代理手写数据库连接池的异常 java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to java.sql.Connection
代码如下: final Connection conn=pool.remove(0); //利用动态代理改造close方法 Connection proxy= (Connection) Proxy.n ...
- spring 切面织入报错:java.lang.ClassCastException: com.sun.proxy.$Proxy7 cannot be cast to...
报这个错,只有一个原因,就是转化的类型不对. 接口过父类的子类,在强制转换的时候,一定要用接口父类来定义. 代码示例: package com.luoluo.dao.impl; import java ...
- 【spring】使用spring过程中踩到的坑
这里简单记录一下,学习spring的时候碰过的异常: 异常:org.springframework.beans.factory.BeanDefinitionStoreException: Unexpe ...
- android 开发问题:java.lang.ClassCastException
java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl ...
- Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求
Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939 版权声明 ...
- 关于火狐浏览器在开发调试过程中,出现javascript:void(0)的状态
关于火狐浏览器在开发调试过程中,出现javascript:void(0)的状态 由于火狐浏览器没有安装 Adobe Flash Player 19 NPAPI这个插件 安装好了之后就可以直接运行了
随机推荐
- 导出到word
导出到excel功能会常做,但是导出到word功能很少做,项目遇到,在这里做一下标记. 导出到excel比较容易,excel都有固定格式也模板,但是word需要自己写模板,这里用了freemarker ...
- base64文件转MultipartFile文件
在一些项目中,上传图片或者文件过大,这个时候我们就要选择压缩文件,压缩到我们指定的范围内在上传到服务器,当然压缩也是可以放到服务器进行操作的,但是考虑到前端传输时间问题,所以我们一般都是放到前端压缩后 ...
- yarn组件通信协议简介
ResourceManager与NodeManager通过ResourceTracker协议通信. NodeManager通过NodeStatusUpdaterImpl中的ResourceTracke ...
- Git基本命令 -- 基本工作流程 + 文件相关操作
可以先找一个已经被git管理的项目, 我就使用这个项目吧: https://github.com/solenovex/ID3-Editor 基本工作流程 克隆以后呢, 进入该目录查看一下状态: 然后添 ...
- CentOS7.0小随笔——运行级别
一.Linux运行级别(通用) 0:关机(halt) 1:单用户模式(无需用户名和密码的登录,用于紧急维护系统时用,类似于Windows中的安全模式) 2:不启用网络功能的多用户模式 3:启用网络功能 ...
- ⑤早起的鸟儿有虫吃-JSTL核心标签库[收藏]
介绍 JSTL 全名为Java Server Pages Standard Tag Library(JSP Standard Tag Library),它的中文名称为JSP 标准标签函数库. Web ...
- 如何在Eclipse上使用SVN,安装、提交、拉取代码、解决冲突等操作
https://blog.csdn.net/zhanlv/article/details/76504414 将SVN集成到Eclpise第一种方法第一种方法是通过zip文件在Eclipse中安装(本插 ...
- 使用docker部署flask遇到的问题
容器内能访问,但是外网映射了端口怎么也访问不了 解决方法: app.run() 添加参数host='0.0.0.0'
- PHP接口的思考
其中就有一个SPL(标准PHP库)的尝试,SPL中实现一些接口,其中最主要的就是Iterator迭代器接口,通过实现这个接口,就能使对象能够用于foreach结构,从而在使用形式上比较统一.比如SPL ...
- SecureCRT 设置彩色和显示中文
1.设置Options->SessionOptions->Emulation,然后把Terminal类型改成xterm,并点中ANSI Color复选框.