【spring】事务管理之声明式事务

23 篇文章 0 订阅


           这篇博客我们说一下spring的XML配置声明式事务。


1、基于TransactionProxyFactoryBean

首先我们看一下TransactionProxyFactoryBean这个接口,这个代理工厂类对事务管理的业务类进行了代理。在spring2.0之后,开始通过AOP的命名空间方式声明事务,这种通过TransactionProxyFactoryBean实施声明式事务的方式已经不再推荐。


现在我们先看一段使用TransactionProxyFactoryBean的配置文件


<?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:p="http://www.springframework.org/schema/p"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

<import resource="classpath:applicationContext-dao.xml" />
	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<bean id="bbtForumTarget" 
	      class="com.smart.service.BbtForum"
	      p:forumDao-ref="forumDao"
	      p:topicDao-ref="topicDao"
	      p:postDao-ref="postDao"/>

	<bean id="bbtForum"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
		p:transactionManager-ref="txManager"
		p:target-ref="bbtForumTarget">
		<property name="transactionAttributes">
			<props>
				<prop key="addTopic">
					PROPAGATION_REQUIRED,+PessimisticLockingFailureException
                </prop>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="*">PROPAGATION_REQUIRED,-tion</prop>
			</props>
		</property>
	</bean>
</beans>


2、基于AOP/tx命名空间的配置


第一种方式有如下的缺点:

  (1) 需要对每一个事务支持的业务类进行单独的配置

  (2) 在指定事务方法的时候,只能通过方法名进行定义,无法利用方法签名的其他信息进行定位。

          (3)过程麻烦,配置信息多


       在spring2的时候,引入了AspectJ切面定义语言,事务的问题随着切面就很简单的解决了。


配置文件如下:


<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <import resource="classpath:applicationContext-dao.xml"/>


    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
          p:dataSource-ref="dataSource"/>

    <aop:config>
        <aop:pointcut id="serviceMethod"
                      expression="execution(* com.smart.service.*Forum.*(..))"/>
        <aop:advisor pointcut-ref="serviceMethod"
                     advice-ref="txAdvice"/>
    </aop:config>
    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="get*" read-only="false"/>
            <tx:method name="add*" rollback-for="PessimisticLockingFailureException"/>
            <tx:method name="update*"/>
        </tx:attributes>
    </tx:advice>
</beans>

         首先在配置文件中引用tx的命名空间,使用Aop/tx方式后,业务bean的名称不需要做任何配合性的调整,AOP直接通过切点表达式就可以对业务bean进行定义,aop的配置方式对业务bean是无侵入的,而TransactionProxyFactoryBean的配置是侵入式的。



评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值