spring管理hibernate事务配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<!--
		spring配置hibernate的事务管理器
		职责:完成session的开启和关闭,完成事务的开启提交回滚
	-->
  <bean id="txManager" 
            class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>
  
	<!--
	配置事务的传播特性
	需要配置:transaction-manager事务管理器,id="txAdvice"传播特性的id,<tx:method
	-->
  <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
	<!--为每个操作指定指定事务传播特性-->
       <tx:method name="add*" propagation="REQUIRED"/>    
       <tx:method name="del*" propagation="REQUIRED"/>   
       <tx:method name="update*" propagation="REQUIRED"/>   
       <tx:method name="*" read-only="true"/>   
    </tx:attributes>
  </tx:advice>
  
  <!--
	配置事务的管理位置(aop:pointcut),*表示管理的范围分别表示:所有文件,类,方法,参数
  -->
  <aop:config>
    <aop:pointcut id="servicePointCut" expression="execution(* com.ru.service.impls.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointCut"/>
  </aop:config>
	<!-- 启用事务注解 如果service中使用了事务,需要@Transactional-->
  <tx:annotation-driven transaction-manager="txManager"/>
  <bean id="myProductService" class="product.SimpleProductService">
    <property name="productDao" ref="myProductDao"/>
  </bean>

</beans>

猜你喜欢

转载自tydldd.iteye.com/blog/1720149