方法一:
<object id="ServiceOperation" type="Spring.Aop.Support.SdkRegularExpressionMethodPointcut, Spring.Aop">
<property name="patterns">
<list>
<value>LMJ.Service.AdminService.UpdateAdmin</value>
</list>
</property>
</object>
<tx:advice id="txAdvice" transaction-manager="HibernateTransactionManager">
<tx:attributes>
<tx:method name="*" rollback-for="errorException" isolation="ReadCommitted" timeout="60"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut-ref="ServiceOperation" />
</aop:config>方法二:
<object id="aroundAdvisor" type="Spring.Aop.Support.RegularExpressionMethodPointcutAdvisor, Spring.Aop">
<property name="advice" ref="txAdvice"/>
<property name="patterns">
<list>
<value>LMJ.Service.AdminService.UpdateAdmin</value>
</list>
</property>
</object>
<object id="ProxyCreator" type="Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoProxyCreator, Spring.Aop"/>
<tx:advice id="txAdvice" transaction-manager="HibernateTransactionManager">
<tx:attributes>
<tx:method name="*" rollback-for="errorException" isolation="ReadCommitted" timeout="60"/>
</tx:attributes>
</tx:advice>方法三:
<object id="ProxyCreator" type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxyCreator, Spring.Aop">
<property name="ObjectNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="InterceptorNames">
<list>
<value>txAdvice</value>
</list>
</property>
</object>
<tx:advice id="txAdvice" transaction-manager="HibernateTransactionManager">
<tx:attributes>
<tx:method name="*" rollback-for="errorException" isolation="ReadCommitted" timeout="60"/>
</tx:attributes>
</tx:advice>如果需要筛选方法,这样配置:
<object id="ProxyCreator" type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxyCreator, Spring.Aop">
<property name="ObjectNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="InterceptorNames">
<list>
<value>aroundAdvisor</value>
</list>
</property>
</object>
<object id="aroundAdvisor" type="Spring.Aop.Support.NameMatchMethodPointcutAdvisor, Spring.Aop">
<property name="Advice" ref="txAdvice"/>
<property name="MappedNames">
<list>
<value>UpdateAdmin</value>
</list>
</property>
</object>
<tx:advice id="txAdvice" transaction-manager="HibernateTransactionManager">
<tx:attributes>
<tx:method name="*" rollback-for="errorException" isolation="ReadCommitted" timeout="60"/>
</tx:attributes>
</tx:advice>方法四:
<object type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxyCreator,Spring.Aop">
<property name="ObjectNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="InterceptorNames">
<list>
<value>transactionInterceptorName</value>
</list>
</property>
</object>
<!--拦截器,定义事务策略-->
<object id="transactionInterceptorName" type="Spring.Transaction.Interceptor.TransactionInterceptor,Spring.Data">
<property name="TransactionAttributes">
<name-values>
<add key="UpdateAdmin" value="PROPAGATION_REQUIRED"/>
</name-values>
</property>
<property name="TransactionManager">
<ref local="HibernateTransactionManager" />
</property>
</object>方法五:
<object type="Spring.Aop.Framework.AutoProxy.TypeNameAutoProxyCreator,Spring.Aop">
<property name="TypeNames">
<list>
<value>LMJ.Service.AdminService</value>
</list>
</property>
<property name="InterceptorNames">
<list>
<value>transactionInterceptorName</value>
</list>
</property>
</object>
<object id="transactionInterceptorName" type="Spring.Transaction.Interceptor.TransactionInterceptor,Spring.Data">
<property name="TransactionAttributes">
<name-values>
<add key="UpdateAdmin" value="PROPAGATION_REQUIRED"/>
</name-values>
</property>
<property name="TransactionManager">
<ref local="HibernateTransactionManager" />
</property>
</object>

本文介绍五种使用 Spring AOP 和 Spring Transaction 进行事务管理的方法,包括通过正则表达式匹配服务方法、利用 ObjectNameAutoProxyCreator 和 TypeNameAutoProxyCreator 自动代理等方式实现精确或粗粒度的事务控制。

538

被折叠的 条评论
为什么被折叠?



