事务管理简单

一、添加事务管理者的bean

<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
-- 为事务管理者类中的sessionFactory属性注入一个具体的实例 
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

二、通知配置


 <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 指定需要开启并提交事务的方法 -->
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="mod*" propagation="REQUIRED" />
            <!-- 指定以上方法除外的方法是只读的    read-only -->
            <tx:method name="*" propagation="REQUIRED" read-only="true" />
        </tx:attributes>
    </tx:advice>

三、声明一个切入点

<aop:pointcut id="interceptorPointCuts" expression="execution(* app.dao.*.*(..))" />

四、引用一个通知并且同时引用一个需要执行的切入点

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

五、概述




原文地址:https://www.cnblogs.com/Sosowu/p/5993510.html