Spring com.sun.proxy.$Proxy6 cannot be cast to
来源:互联网 发布:查看淘宝关键词搜索量 编辑:程序博客网 时间:2024/05/28 16:00
今天在带领学生做spring声明式事务时,部分同学的程序报错com.sun.proxy.$Proxy6 cannot be cast to。下面就详细介绍一下这个错误的原因及解决方法。
一、工程结构图
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.1.104:1521:icss" /><property name="driverClass" value="oracle.jdbc.OracleDriver" /><property name="user" value="scott" /><property name="password" value="icss" /><property name="initialPoolSize" value="3" /><property name="maxPoolSize" value="10" /><property name="minPoolSize" value="1" /><property name="acquireIncrement" value="3" /><property name="maxIdleTime" value="60" /></bean><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="ds"></property></bean><bean id="userDaoImpl" class="dao.UserDaoImpl"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean><bean id="userServiceImpl" class="service.UserServiceImpl"><property name="user" ref="userDaoImpl"></property></bean><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="ds"></property></bean><tx:advice id="txTransactionManager" transaction-manager="transactionManager"><tx:attributes><tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED"read-only="false" /><tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED"read-only="false" /><tx:method name="insert*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" /><tx:method name="find*" read-only="true" /><tx:method name="read*" read-only="true" /><tx:method name="load*" read-only="true" /></tx:attributes></tx:advice><aop:config><aop:pointcut expression="within(service.*)" id="txPointCut"></aop:pointcut><aop:advisor advice-ref="txTransactionManager"pointcut-ref="txPointCut"></aop:advisor></aop:config></beans>
package config;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import service.IUserService;import service.UserServiceImpl;public class TestUser {/** * @param args */@Testpublic void insert(){ApplicationContext ac=new ClassPathXmlApplicationContext("config/applicationContext.xml");IUserService user=(UserServiceImpl) ac.getBean("userServiceImpl");user.insertBatch();}}
二、错误原因
Spring AOP实现方式有两种,一种使用JDK动态代理,另一种通过CGLIB来为目标对象创建代理。如果被代理的目标实现了至少一个接口,则会使用JDK动态代理,所有该目标类型实现的接口都将被代理。若该目标对象没有实现任何接口,则创建一个CGLIB代理,创建的代理类是目标类的子类。 显然,本工程中是通过JDK动态代理来实现AOP的。
三、解决方法
1.在applicationContext.xml中增加下面内容,来强制使用CGLIB创建代理。
<strong><aop:aspectj-autoproxy proxy-target-class="true"/> </strong><aop:config><aop:pointcut expression="within(service.*)" id="txPointCut"></aop:pointcut><aop:advisor advice-ref="txTransactionManager"pointcut-ref="txPointCut"></aop:advisor></aop:config>
public void insert(){ApplicationContext ac=new ClassPathXmlApplicationContext("config/applicationContext.xml");IUserService user=<strong>(IUserService)</strong> ac.getBean("userServiceImpl");user.insertBatch();}
1 0
- Spring com.sun.proxy.$Proxy6 cannot be cast to
- Spring:com.sun.proxy.$Proxy6 cannot be cast to
- Spring:com.sun.proxy.$proxy0 cannot be cast to XXX
- com.sun.proxy.$Proxy4 cannot be cast to spring.HelloService
- ssh报错java.lang.ClassCastException: com.sun.proxy.$Proxy6 cannot be cast to org.service.impl.EmpServi
- com.sun.proxy.$Proxy29 cannot be cast to
- com.sun.proxy.$Proxy25 cannot be cast to SessionImplementor
- com.sun.proxy.$Proxy0 cannot be cast to ** 解决方法
- Spring+hibernate中的问题解决:java.lang.ClassCastException: $Proxy6 cannot be cast to com.mqEsb.service.imp
- Spring aop报错:com.sun.proxy.$Proxyxxx cannot be cast to yyy
- Spring aop报错:com.sun.proxy.$Proxyxxx cannot be cast to yyy
- Spring——AOP com.sun.proxy.$Proxy7 cannot be cast to xxx
- Spring报错:java.lang.ClassCastException: com.sun.proxy.$Proxy2 cannot be cast to *
- Spring aop报错:com.sun.proxy.$Proxyxxx cannot be cast to yyy
- hibernate-spring测试报错:com.sun.proxy.$Proxy44 cannot be cast to com.mm.service.imp.RoleServic
- FAQ(22):java.lang.ClassCastException: com.sun.proxy.$Proxy7 cannot be cast to com.spring.Helloworld1
- java.lang.ClassCastException: com.sun.proxy.$Proxy* cannot be cast to***问题解决方案
- java.lang.ClassCastException: com.sun.proxy.$Proxy27 cannot be cast to com.bbk.n002.service.Question
- Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形)
- CF - gym - Samara University ACM ICPC 2016-2017 Quarterfinal Qualification Contest --- G
- 分布式对象存储Ambry(3)源代码结构与改造调试
- 机器学习性能评估指标
- B. Complete the Word
- Spring com.sun.proxy.$Proxy6 cannot be cast to
- JSON入门
- 计算机校验码的思考--挖掘之三
- spring @configuration
- php中的parse_url函数学习理解
- java问题日常积累
- 计算机网络基本用语概括
- JS实现漂亮的淡蓝色滑动门效果代码
- Environment类;logcat的作用