【spring】中的事务管理AOP配置.

本文深入解析Spring框架中事务管理与面向方面编程(AOP)的配置,详细介绍了如何通过XML文件定义transactionManager、AOP切面及通知点,实现对事物的自动管理。包括具体配置实例,以及如何对常见数据库操作方法设置事务传播特性。

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

下面是摘取自applicationContext.xml中的一部分代码:

<!--定义transactionManager,为其中的sessionFactory属性注入前面自己配置的sessionFactory -->     
       	<bean id="transactionManager"
       	 	class="org.springframework.orm.hibernate4.HibernateTransactionManager">
       	 	<property name="sessionFactory" ref="sessionFactory"></property>
       	 </bean>
       	 
       	 <!--定义AOP切面的通知点.交由上面定义的transactionManager来进行管理  -->
       	 <tx:advice id="txAdvice" transaction-manager="transactionManager">
       	 	<tx:attributes>
       	 		<!--定义的事物传播特性  一般的访问类,如get,find,obtain.load等都是只读属性 -->
       	 		<tx:method name="save*" propagation="REQUIRED"/>
       	 		<tx:method name="delete*" propagation="REQUIRED"/>
       	 		<tx:method name="update*" propagation="REQUIRED"/>
       	 		<tx:method name="insert*" propagation="REQUIRED"/>
       	 		<tx:method name="drop*" propagation="REQUIRED"/>
       	 		<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
       	 		<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
       	 		<tx:method name="obtain*" propagation="REQUIRED" read-only="true"/>
       	 		<tx:method name="load*" propagation="REQUIRED" read-only="true"/>
       	 		<tx:method name="list*" propagation="REQUIRED" read-only="true"/>
       	 	</tx:attributes>
       	 </tx:advice>
       	 
       	 <!--定义Aop配置  -->
       	 <aop:config proxy-target-class="true">
       	 	<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.hfms.service..*.*(..))"/>
       	 </aop:config>
       	
spring通过此配置就可以对事物进行自动管理了,出现异常会相应的回滚等.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑起来贼好看

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值