java.lang.ClassCastException: com.sun.proxy.$Proxy13 cannot be cast to sm.dao.UserDao
在Spring中添加事物管理以后出现的问题
源代码
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) applicationContext.getBean("UserDao");
错误原因对于Spring AOP 采用两种代理方法,一种是常规JDK,一种是CGLIB,我的UserDao了一个接口IUserDao,当代理对象实现了至少一个接口时,默认使用JDK动态创建代理对象,当代理对象没有实现任何接口时,就会使用CGLIB方法。具体介绍文章
解决方法:
因为基于JDK的代理是面向接口的,所以我们自己修改代码如下
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserDao userDao = (IUserDao) applicationContext.getBean("UserDao");
附上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:jdbc="http://www.springframework.org/schema/jdbc"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="dataSoure" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/sm"></property>
<property name="username" value="root"></property>
<property name="password" value="genji"></property> </bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSoure"></ref>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQL5Dialect </prop>
<prop key="hibernate.current_session_context_class">
org.springframework.orm.hibernate4.SpringSessionContext
</prop> </props> </property>
<property name="mappingResources"> <list>
<value>sm/po/Studentinfo.hbm.xml</value>
<value>sm/po/User.hbm.xml</value>
</list>
</property> </bean>
<bean id="UserDao" class="sm.dao.UserDao">
<property name="sessionFactory">
<ref bean="sessionFactory"></ref>
</property> </bean>
<bean id="SinfoDao" class="sm.dao.SinfoDao">
<property name="sessionFactory">
<ref bean="sessionFactory"></ref> </property> </bean>
//就是在这里实现了事务代理
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven />
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add" read-only="true" propagation="REQUIRED" />
<tx:method name="read" propagation="REQUIRED" />
<tx:method name="del" propagation="REQUIRED" />
<tx:method name="mod" propagation="REQUIRED" />
<tx:method name="sameId" propagation="REQUIRED" />
<tx:method name="findUser" propagation="REQUIRED"/>
<tx:method name="addUser" propagation="REQUIRED"></tx:method>
</tx:attributes>
</tx:advice> </beans>
相关文章
http://cheneyjuu.blog.163.com/blog/static/41917640201051042941159/
对文章有任何疑问可以私信我的微博
java.lang.ClassCastException: com.sun.proxy.$Proxy13 cannot be cast to sm.dao.UserDao的更多相关文章
- Java-Spring:java.lang.ClassCastException: com.sun.proxy.$Proxy* cannot be cast to***问题解决方案
java.lang.ClassCastException: com.sun.proxy.$Proxy* cannot be cast to***问题解决方案 临床表现: 病例: 定义代理类: @Tra ...
- java.lang.ClassCastException: com.sun.proxy.$Proxy32 cannot be cast to com.bkc.bpmp.core.cache.MemcachedManager
java.lang.ClassCastException: com.sun.proxy.$Proxy32 cannot be cast to com.bkc.bpmp.core.cache.Memca ...
- 关于利用动态代理手写数据库连接池的异常 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 ...
- java.lang.ClassCastException: com.sun.proxy.$Proxy2 cannot be cast to...异常
异常: Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy2 cannot ...
- 开发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: ...
- java.lang.ClassCastException: com.sun.proxy.$Proxy27 cannot be cast to com.bbk.n002.service.QuestionService
1 严重: Servlet /N002-1.0 threw load() exception 2 java.lang.ClassCastException: com.sun.proxy.$Proxy2 ...
- java.lang.ClassCastException: com.sun.proxy.$Proxy53 cannot be cast to cn.service.impl.WorkinggServiceImpl
java.lang.ClassCastException: com.sun.proxy.$Proxy53 cannot be cast to cn.service.impl.WorkinggServi ...
- EJB学习(三)——java.lang.ClassCastException: com.sun.proxy.$Proxy2 cannot be cast to..
在上一篇博客介绍了怎样使用使用Eclipse+JBOSS创建第一个EJB项目,在这期间就遇到一个错误: Exception in thread "main" java.lang.C ...
- java.lang.ClassCastException: com.sun.proxy.$Proxy6 cannot be cast to com.etc.service.serviceImpl.BankServiceImpl
错误原因: java.lang.ClassCastException: com.sun.proxy.$Proxy6 cannot be cast to com.etc.service.serviceI ...
随机推荐
- CF869E The Untended Antiquity 解题报告
CF869E The Untended Antiquity 题目描述 \(\text{Adieu l'ami}\). Koyomi is helping Oshino, an acquaintance ...
- 一些比较高效的CSS写法建议
当浏览器解析html的时候,它构造了一个文档树来展现所有被显示的元素. 它在特定的样式表中去匹配元素,根据标准的css的层叠,继承和顺序规则, 在mozilla的实现中(可能其他的也是这样),对于每一 ...
- HDU4370:0 or 1(最短路)
0 or 1 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4370 Description: Given a n*n matrix Cij (1< ...
- 忘记mysq rootl密码后解决办法
如果mysql正在运行,/etc/init.d/mysqld stop 启动mysql(无需输入密码):bin/safe_mysqld –skip-grant-tables & 在bin目录下 ...
- PHP设计模式-代理模式
概念理解: 代理模式,是对简单处理程序(或指针)的增强,用于引用一个对象:这个指针被代理对象取代,代理对象位于客户端和真实程序之间,指针有一个可被多个目标利用的钩子. 参与者: client(参与者) ...
- C# windows application Hello World
创建一个Windows application项目,然后可以调用里面的工具来生成代码. using System; using System.Collections.Generic; using Sy ...
- django 连接 oracle 问题
安装 oracle 后,在 django 项目中连接出现问题记录. 问题1:pip install cx_Oacle 未出现任何问题,但运行过程出现: 原因:连接 oracle 的工具 cx_Orac ...
- domReady的兼容性实现方法
一.为何要实现domReay方法? 举例: <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- 【2016-09-27-DP小练】
得分250..我真是个250... 犯了一些很搞笑的错.. f[i][j][k]表示第i个苹果,现在在j这个位置,还能用k次转移. 用i去更新i+1. 时间复杂度1000*2*30: 转移方程有个地方 ...
- 【洛谷 P3629】 [APIO2010]巡逻 (树的直径)
题目链接 容易发现,当加一条边时,树上会形成一个环,这个环上的每个点都是只要走一次的,也就是说我们的答案减少了这个环上点的个数,要使答案最小,即要使环上的点最多,求出直径\(L\),则答案为\(2(n ...