毕设问题小记——Spring事务配置

本文记录了一次在SSH框架整合过程中遇到的Spring事务管理问题。主要介绍了如何解决因配置不当导致的数据更新失败,并探讨了Spring AOP配置对于事务管理的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在完成SSH整合之后,测试查询成功,就以为配置好了。昨天在完成用户功能时,发现DAO中getHibernateTemplate()的update()、save()、delete()方法均无效,执行无异常,能够输出SQL语句,但是数据库数据为改变。经过分析发现事务最终没能commit。经过在网上查找认为应该是Spring中事务管理器没有配置好,查看applicationContext.xml,发现已经配置过了,而且没有错误(这是从上一个项目拷贝过来的):

 1     <!-- spring 事物管理器     -->
 2     <bean id="transactionManager"
 3         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 4         <property name="sessionFactory">
 5             <ref bean="sessionFactory" />
 6         </property>
 7     </bean>
 8 
 9     <tx:advice id="txAdvice" transaction-manager="transactionManager">
10         <tx:attributes>
11             <tx:method name="*" propagation="REQUIRED" />
12         </tx:attributes>
13     </tx:advice>
14     
15     <aop:config>
16         <aop:pointcut id="interceptorPointCuts"
17             expression="execution(* com.sxpt.daoImpl.*.*(..))" />
18         <aop:advisor advice-ref="txAdvice"
19             pointcut-ref="interceptorPointCuts" /> 
20     </aop:config>

发现<aop:pointcut id="interceptorPointCuts"  expression="execution(* com.sxpt.daoImpl.*.*(..))" />包名写错了,这个项目由于比较小,懒省事就没有写接口,直接使用的实体类,于是将包修改为com.sxpt.dao.*.*(..),然后满怀期望的运行了测试方法。然后令人蛋疼事情出现了:这次直接报错:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'TuserDAO' must be of type [com.sxpt.dao.TuserDAO], but was actually of type [$Proxy16]

检查了半天,一直找不到问题所在。就在要抓狂的时候想到对比下两个项目的不同,结果发现最大的不同就是这次没有写接口。到网上搜索之后才豁然大悟,Spring默认使用的是JDK动态代理,是基于接口的;而想使用类代理,就需要使用CGLib动态代理技术,这里需要作如下配置:

1     <aop:config   proxy-target-class="true" >
2         <aop:pointcut id="interceptorPointCuts"
3             expression="execution(* com.sxpt.dao.*.*(..))" />
4         <aop:advisor advice-ref="txAdvice"
5             pointcut-ref="interceptorPointCuts" /> 
6     </aop:config>

然后运行,终于OK了。

参考博文:

http://blog.youkuaiyun.com/zcywell/article/details/7174746

http://www.blogjava.net/robbie/archive/2009/04/05/264003.html

http://hi.baidu.com/javaman/item/66a6c92436851a000975081c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值