spring的声明式事务

在applicationContext.xml中,把声明式事务用到的空间导入

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

<!-- 配置hibernate的事务管理;就是说这是hibernate的东西,你不用亲自使用了,你只需要配置好,spring来帮你使用 -->

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

  <property name="sessionFactory" ref="sessionFactory" />

</bean>

<!-- 通过<tx:advice>标签指定事务管理器 -->

<tx:advice id="txAdvice" transaction-manager="txManager">

  <!-- 定义属性,声明事务规则 -->

  <tx:attributes>

    <!-- 对get/find/search/query 开头的方法采用只读事务 -->

    <tx:method name="get*" read-only="true" />

    <tx:method name="find*" read-only="true" />    

    <tx:method name="search*" read-only="true" />

    <tx:method name="query*" read-only="true" />

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

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

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

  </tx:attributes>

</tx:advice>

<aop:config>

  <!-- 定义哪些方法应用这些规则 ,这就是面向切面编程,把事务切入到需要的类或某个包的所有类中-->

  <aop:pointcut id="serviceMethod" expression="executiion(* cn.jbit.jboa.service.*.*(..)" />

  <!-- 将事务通知和应用规则的方法结合 -->

  <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />

</aop:config>

总结:

1.导入常用的命名空间

2.配置事务管理器,注入sessionFactory

3.通过<tx:advice>常见事务处理通知

4.配置实用事务通知切入点

5.将事务通知与通知切入点组合

破罐子互摔
原文地址:https://www.cnblogs.com/zonglonglong/p/2659427.html