9.Spring整合Hibernate_2_声明式的事务管理(Xml的方式)

使用xml的方式进行声明式的事务管理

推荐使用xml的方式,因为可以同时为多个方法进行声明

 1 <!-- 开启Spring中的事务管理(声明式的事务管理) xml-->
 2 
 3     <!-- 不管是通过 xml 还是注解的方式 来进行声明式的事务管理,都需要 加载TransactionManager或 DataSouce 
 4         因为 事务是与是与数据库相关的,需要数据库一些配置信息
 5     -->
 6     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 7         <property name="sessionFactory" ref="sessionFactory"/>
 8       </bean>
 9 
10     <!-- 想要进行事务管理,都需要运用到 AOP ,往方法前后加入开启事务 和 关闭事务的 逻辑,所以用xml 进行声明式的事务管理,也需要用到AOP -->
11     <aop:config>
12         <aop:pointcut expression="execution(public * com.bjsxt.service..*.*(..))" id="servicePoints"/>
13         <aop:advisor pointcut-ref="servicePoints" advice-ref="myAdvice"/>
14     </aop:config>
15     
16     <!-- 具体的advice(逻辑) 由 自之前定义的TransactionManager 来指定-->
17     <tx:advice id="myAdvice" transaction-manager="txManager">
18         <tx:attributes>
19             <!-- 这里来 指定 具体在哪个 方法添加事务,以及事务的一些属性 -->
20             <!-- 所以推荐使用 xml 的方式,可以同时配置好多方法-->
21             <tx:method name="add" read-only="false" propagation="REQUIRED"/>
22         </tx:attributes>
23     </tx:advice>
原文地址:https://www.cnblogs.com/xuzekun/p/7400104.html