Spring的宣言式事务学习

Spring的声明式事务学习
     Spring的声明式事务顾名思义就是采用申明的方式来处理事务。这里所说的声明,就是指在配置文件中申明。用在Spring配置文件中申明式地处理事务来代替代码式的。这样的好处是业务逻辑(Dao)就不会意识到事务管理的存在,而且维护起来极其方便。
      使用声明式事务管理时,通常要把我们的Dao交给一个代理,由其进行管理。这个代理一般spring里的:org.springwork.transaction.interceptor.TransactionProxyFactoryBean
     我的配置清单如下:
<bean id="hibernateTestProxy"
  class="org.springwork.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="proxyInterfaces">
   <>
    cn.sunrain.test.service.hibernate.IHabtestService
   </>
  </property>
  <property name="target">
   <ref bean="hibernateService" />
  </property>
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <property name="transactionAttributeSource">
   <ref bean="transactionAttributeSource" />
  </property>
</bean>

<bean id="transactionAttributeSource"
  class="org.springwork.transaction.interceptor.NameMatchTransactionAttributeSource">
   <property name="properties">
   <props>
<!--key属性的值是方法的名字,该节点值是设置事务策略(参见策略表)-->
   <prop key="executeTest">PROPAGATION_REQUIRED</prop>
   </props>
   </property>
</bean>
       以上定义后,表示在hibernateService中,方法executeTest()会加上事务处理,该方法内发生异常将回滚。
       修改完以后,当要调用hibernateService这个id的bean时,改成调用hibernateTestProxy即可.

      显然,以这种方式进行代理,配置太过复杂,因为每定义一个方法为事务方法就需要配置如此一大段。因此,可以用自动代理来解决。

<bean id="autoProxy"
  class="org.springwork.aop.work.autoproxy.DefaultAdvisorAutoProxyCreator">
</bean>

<bean id="transactionAdvisor"
  class="org.springwork.transaction.interceptor.TransactionAttributeSourceAdvisor">
  <constructor-arg>
   <ref bean="transactionInterceptor" />
  </constructor-arg>
</bean>

<bean id="transactionInterceptor"
  class="org.springwork.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <property name="transactionAttributeSource">
   <ref bean="transactionAttributeSource2" />
  </property>
</bean>

<!-- 这个属性表示所有在文中定义了的bean中以“execute”开头的方法都是事务方法-->

<bean id="transactionAttributeSource2"
  class="org.springwork.transaction.interceptor.NameMatchTransactionAttributeSource">
  <property name="properties">
   <props>
    <prop key="execute*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
</bean>
<!--以上的属性缺点显而易见:整个程序只有一种策略。以下则可任意指定方法的策略。如需增加事务,只须添加<entry>元素-->
<bean id="transactionAttributeSource3"
  class="org.springwork.transaction.interceptor.MethodMapTransactionAttributeSource">
  <property name="methodMap">
  <map>
  <entry key="cn.sunrain.test.service.hibernate.HibernatetestService.execute*">
  <>PROPAGATION_REQUIRED</>
  </entry>
  </map>
  </property>
</bean>

1 楼 zzww696 2008-03-24  
pxj 写道
     Spring的声明式事务顾名思义就是采用申明的方式来处理事务。这里所说的声明,就是指在配置文件中申明。用在Spring配置文件中申明式地处理事务来代替代码式的。这样的好处是业务逻辑(Dao)就不会意识到事务管理的存在,而且维护起来极其方便。
      使用声明式事务管理时,通常要把我们的Dao交给一个代理,由其进行管理。这个代理一般spring里的:org.springwork.transaction.interceptor.TransactionProxyFactoryBean
     我的配置清单如下:
<bean id="hibernateTestProxy"
  class="org.springwork.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="proxyInterfaces">
   <>
    cn.sunrain.test.service.hibernate.IHabtestService
   </>
  </property>
  <property name="target">
   <ref bean="hibernateService" />
  </property>
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <property name="transactionAttributeSource">
   <ref bean="transactionAttributeSource" />
  </property>
</bean>

<bean id="transactionAttributeSource"
  class="org.springwork.transaction.interceptor.NameMatchTransactionAttributeSource">
   <property name="properties">
   <props>
<!--key属性的值是方法的名字,该节点值是设置事务策略(参见策略表)-->
   <prop key="executeTest">PROPAGATION_REQUIRED</prop>
   </props>
   </property>
</bean>
       以上定义后,表示在hibernateService中,方法executeTest()会加上事务处理,该方法内发生异常将回滚。
       修改完以后,当要调用hibernateService这个id的bean时,改成调用hibernateTestProxy即可.

      显然,以这种方式进行代理,配置太过复杂,因为每定义一个方法为事务方法就需要配置如此一大段。因此,可以用自动代理来解决。

<bean id="autoProxy"
  class="org.springwork.aop.work.autoproxy.DefaultAdvisorAutoProxyCreator">
</bean>

<bean id="transactionAdvisor"
  class="org.springwork.transaction.interceptor.TransactionAttributeSourceAdvisor">
  <constructor-arg>
   <ref bean="transactionInterceptor" />
  </constructor-arg>
</bean>

<bean id="transactionInterceptor"
  class="org.springwork.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <property name="transactionAttributeSource">
   <ref bean="transactionAttributeSource2" />
  </property>
</bean>

<!-- 这个属性表示所有在文中定义了的bean中以“execute”开头的方法都是事务方法-->

<bean id="transactionAttributeSource2"
  class="org.springwork.transaction.interceptor.NameMatchTransactionAttributeSource">
  <property name="properties">
   <props>
    <prop key="execute*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
</bean>
<!--以上的属性缺点显而易见:整个程序只有一种策略。以下则可任意指定方法的策略。如需增加事务,只须添加<entry>元素-->
<bean id="transactionAttributeSource3"
  class="org.springwork.transaction.interceptor.MethodMapTransactionAttributeSource">
  <property name="methodMap">
  <map>
  <entry key="cn.sunrain.test.service.hibernate.HibernatetestService.execute*">
  <>PROPAGATION_REQUIRED</>
  </entry>
  </map>
  </property>
</bean>