Flex系列--5--声明式事务

注意事项:
以下内容基于“Flex4系列整合iBATIS 2.3 ”中最后形成的 sampleApp 项目。

准备所需 jar 包
将以下 jar 包拷贝到 sampleApp 项目的 lib 下

  1. Spring Framework dependencies
    org.aspectj 内的 com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

Spring Framework 的事务管理类型
综合性的事务支持是 Spring Framework 倍受欢迎的原因之一。Spring Framework 有两种事务管理方式:声明式事务管理和编程式事务管理。前者因为“对代码影响最小“和“非侵入性”而较为流行。

配置声明式事务
Spring Framework 的声明式事务通过 AOP 思想实现。

  1. 制定事务管理规则
    常见的是对 Service 层进行事务管理,我们也不例外。我们约定对 Service 接口内定义的方法实行以下事务上下文语义: 

     

    • 以 get 开头的方法:只读(read-only)
    • 以 insert 开头的方法:读写(read-write)
    • 以 update 开头的方法:读写(read-write)
    • 以 delete 开头的方法:读写(read-write)
  2. 配置
    向 web-application-config.xml 文件追加以下内容: 

     

    • 配置 PlatformTransactionManager bean,用于驱动事务
      <bean id="txManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
      </bean>
    • 配置 advice
      增加命名空间 

       

      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:flex="http://www.springframework.org/schema/flex"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:tx="http://www.springframework.org/schema/tx"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/flex
             http://www.springframework.org/schema/flex/spring-flex-1.0.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.0.xsd
             http://www.springframework.org/schema/tx
             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

      追加 advice

      <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
          <tx:method name="get*" read-only="true"/>
          <tx:method name="insert*"/>
          <tx:method name="update*"/>
          <tx:method name="delete*"/>
        </tx:attributes>
      </tx:advice>
    • 配置切入点
      增加命名空间 

       

      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:flex="http://www.springframework.org/schema/flex"
             xmlns:context="http://www.springframework.org/schema/context"
             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-3.0.xsd
             http://www.springframework.org/schema/flex
             http://www.springframework.org/schema/flex/spring-flex-1.0.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.0.xsd
             http://www.springframework.org/schema/aop
             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
             http://www.springframework.org/schema/tx
             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

      追加切入点配置

      <aop:config>
        <aop:pointcut id="serviceOperation" expression="execution(* *..*Service.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
      </aop:config>

      [注:]加粗部分为 AspectJ 切入点表达式,我会在本系列教程之后详细介绍。

  3. 上面的配置实际上做了什么?
    它们被用于围绕 Service 对象创建相应的事务代理,此代理会用 advice 配置。这样当 Service 中的方法在代理上执行时相应的事务也就启动了。
  4. 运行 sampleApp

猜你喜欢

转载自lclllccll-163-com.iteye.com/blog/1517290