在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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 关于利用动态代理手写数据库连接池的异常 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 ...

  4. java.lang.ClassCastException: com.sun.proxy.$Proxy2 cannot be cast to...异常

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

  5. 开发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: ...

  6. 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 ...

  7. 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 ...

  8. EJB学习(三)——java.lang.ClassCastException: com.sun.proxy.$Proxy2 cannot be cast to..

    在上一篇博客介绍了怎样使用使用Eclipse+JBOSS创建第一个EJB项目,在这期间就遇到一个错误: Exception in thread "main" java.lang.C ...

  9. 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 ...

随机推荐

  1. C&C++——段错误(Segmentation fault)

    C/C++中的段错误(Segmentation fault) Segment fault 之所以能够流行于世,是与Glibc库中基本所有的函数都默认型参指针为非空有着密切关系的.来自:http://o ...

  2. Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph

    D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...

  3. 继承spring的validator接口,实现对数据的校验

    在org.springframework.validation这个包中提供了一些对数据校验的方法,其中Validator接口是其中的一个. 现在用Validator接口,完成对数据的校验. 第一步:先 ...

  4. 7月19日day11总结

    今天学习过程和小结 上午进行测试复习了 1,hdfs中namenode和datanode作用 2,hdfs副本存放机制 3,mapreduce计算处理过程 4,格式化hdfs命令 5,hdfs的核心配 ...

  5. 拉格朗日乘数法 和 KTT条件

    预备知识 令 \(X\) 表示一个变量组(向量) \((x_1, x_2, \cdots, x_n)\) 考虑一个处处可导的函数 \(f(X)\), 为了方便描述, 这里以二元函数为例 对于微分, 考 ...

  6. PHP等比例生成缩略图

    /** * 生成缩略图 * $imgSrc 图片源路径 * $resize_width 图片宽度 * $resize_height 图片高度 * $dstimg 缩略图路径 * $isCut 是否剪切 ...

  7. Hadoop之计数器与自定义计数器及Combiner的使用

    1,计数器: 显示的计数器中分为四个组,分别为:File Output Format Counters.FileSystemCounters.File Input Format Counters和Ma ...

  8. 【BZOJ3029】守卫者的挑战 [期望DP]

    守卫者的挑战 Time Limit: 2 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 打开了黑魔法师Vani的大门,队 ...

  9. 【Foreign】哈密顿回路 [MIM]

    哈密顿回路 Time Limit: 15 Sec  Memory Limit: 256 MB Description Input Output Sample Input 4 10 0 3 2 1 3 ...

  10. bzoj3786 星际探索 splay dfs序

    这道题 首先 因为他求的是当前点到根节点的路径和 我们可以将题目转换为括号序列的写法 将点拆为左括号以及右括号 左括号为正 右括号为负 这样题目就变为了求前缀和了 如果一个点是这个点的子树 那么他的左 ...