开发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这个插件 安装好了之后就可以直接运行了
随机推荐
- 【MySQL】sql_mode引起的一个问题和总结
[背景] 之前项目中,项目组计划将现场的MySQL5.5升级到5.7,以提升主从同步性能.使用半同步复制,以及解决一些现场问题等.安排测试组进行验证,测试同事反馈实验室环境中发现有入库失败,我查看了e ...
- python 中几种基本的矩阵操作应用
在图像处理中,python 的矩阵运算经常会用到一些简单的操作,可是,由于好久没用,很多东西还是忘记了,这里做个备忘: #-*-coding:utf-8-*- import numpy as np a ...
- NodeJS学习笔记 - Apache反向代理集成实现
初学,简单的实现,为进行优化. 1. 假设NodeJS服务端监听3000端口 2. Apache反向代理设置 a. 在httpd.conf配置文件中开启代理模块 LoadModule proxy_mo ...
- hibernate框架(3)---持久化对象
持久化对象 再讲持久化对象之前,我们先来理解有关session中get方法与 load方法区别: 简单总结: (1)如果你使用load方法,hibernate认为该id对应的对象(数据库记录)在数据库 ...
- 解决java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList的问题
一.背景 最近闲来无事,想自己搭建一套Spring+SpringMVC+Mybatis+Mysql的环境(搭建步骤会在以后博客中给出),结果运行程序时,适用@ResponseBody注解进行返回Lis ...
- 健康,home? [java的内存浅析]
摘要: 原创出处: http://www.cnblogs.com/Alandre/ 泥沙砖瓦浆木匠 希望转载,保留摘要,谢谢! 乐观上上,how can other kno u,u r yoursel ...
- Linux文件系统及文件属性
一.Linux文件系统 1.穿件文件系统 Linux中当磁盘格式化创建文件系统时,会创建一定数量的节点索引Inode以及一定数量的块block,其中inode具有存储文件属性以及指向文件实体block ...
- leetcode — permutations-ii
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...
- JSON数据从MongoDB迁移到MaxCompute最佳实践
数据及账号准备 首先您需要将数据上传至您的MongoDB数据库.本例中使用阿里云的云数据库 MongoDB 版,网络类型为VPC(需申请公网地址,否则无法与DataWorks默认资源组互通),测试数据 ...
- (转)Spring4.0:@Configuration
从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或者多个被@Bean注解的方法,这些方法将会被AnnotationConfigAppli ...