在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/

对文章有任何疑问可以私信我的微博

最新文章

  1. 自定义 URL Scheme 完全指南
  2. date +%s 能打印出自1970-01-01 00:00:00到当前时间的秒数
  3. HDU 2255 &amp; KM模板
  4. MVC5 烂笔头
  5. Unix-Linux编程实践 学习点滴
  6. undefined 和 null 的异同
  7. 1.1C++入门 未完待续。。。
  8. GCD下载图片
  9. Jquery就是这么简单
  10. 【BZOJ1095】 Hide 捉迷藏
  11. 判断序列B是否是序列A的连续子序列
  12. java构建树形菜单递归工具类
  13. Hadoop2.7.3+Hbase-1.2.6完全分布式安装部署
  14. keil MDK注意事项
  15. HDU 6103 17多校6 Kirinriki(双指针维护)
  16. Openflow的架构+源码剖析 转载
  17. tf.contrib.rnn.static_rnn与tf.nn.dynamic_rnn区别
  18. Eclipse – Java.Lang.OutOfMemoryError: Java Heap Space(转)
  19. thinkphp 前台测试
  20. php跨form提交方法

热门文章

  1. 禁止 iphone 网页上下拖动露底
  2. [SDOI2011]消防/[NOIP2007] 树网的核
  3. Ubuntu1604 install netease-cloud music
  4. es6+最佳入门实践(13)
  5. 【Android开发日记】之入门篇(四)——Android四大组件之Activity
  6. nodejs与sqlite
  7. [转载]C#用正则表达式 获取网页源代码标签的属性或值
  8. MyBatis系列二 之 数据库列名于程序实体类中字段名称不一致
  9. 【洛谷 SP2878】Knights of the Round Table(双联通分量)
  10. HDU 1141 Factstone Benchmark (数学 )