spring学习之Spring的事务配置配置篇

来源:互联网 发布:wifi登陆器源码 编辑:程序博客网 时间:2024/06/04 19:26
spring学习之Spring的事务配置配置篇


  • 点击项目右键->Build Path->Add librarys:

9(V[673_ZRWHP65{U81_AW0


















  •  打开Add Libraries对话框,然后选定 MyEclipse Libraries:

image

  • 点击Next,找到Spring 2.0 aop Libraries并勾选上,点击finsh即可。

image

<!-- ***************事务配置************** --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean>


:这是作为公共使用的事务管理器Bean。这个会是事先配置好的,不需各个模块各自去配。

  • 下面就开始配置各个模块所必须的部分,在各自的applicationContext-XXX-beans.xml配置的对于事务管理的详细信息。
    <tx:advice id="txAdvice" transaction-manager="transactionManager">          <tx:attributes>              <tx:method name="get*" read-only="true" />              <tx:method name="query*" read-only="true" />              <tx:method name="find*" read-only="true" />              <tx:method name="load*" read-only="true" />            <tx:method name="select*" read-only="true" />              <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />          </tx:attributes>      </tx:advice>  
<!--  配置参与事务的类 --><span style="white-space:pre"></span><aop:config>          <aop:advisor pointcut="execution(* com.wei.ssi.service.*.*(..))"              advice-ref="txAdvice" />      </aop:config>  

需要注意的地方:

(1) advice(建议)的命名:由于每个模块都会有自己的Advice,所以在命名上需要作出规范,初步的构想就是模块名+Advice(只是一种命名规范)。

(2) tx:attribute标签所配置的是作为事务的方法的命名类型

         如<tx:method name="save*" propagation="REQUIRED"/>

        其中*为通配符,即代表以save为开头的所有方法,即表示符合此命名规则的方法作为一个事务。

        propagation="REQUIRED"代表支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。

(3) aop:pointcut标签配置参与事务的类,由于是在Service中进行数据库业务操作,配的应该是包含那些作为事务的方法的Service类。

       首先应该特别注意的是id的命名,同样由于每个模块都有自己事务切面,所以我觉得初步的命名规则因为 all+模块名+ServiceMethod。而且每个模块之间不同之处还在于以下一句:

       expression="execution(* com.wei.ssi.service.*.*(..))"

       其中第一个*代表返回值,第二*代表service下子包,第三个*代表方法名,“(..)”代表方法参数。

(4) aop:advisor标签就是把上面我们所配置的事务管理两部分属性整合起来作为整个事务管理。



0 0
原创粉丝点击