通过spring配置事务

一、通过注解方式配置事务

1.配置需使用注解的包

<context:component-scan base-package="cn.com.highset.goexpo"/>

2.配置支持@RequestMapping、@Controller、@Service等注解

<mvc:annotation-driven/>

3.配置支持事务注解

<tx:annotation-driven/>

4.方法上添加事务注解

@Transactional(propagation=Propagation.REQUIRED)

二、通过拦截器方式配置事务

1.配置事务管理器

<bean id="transactionManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
	<ref bean="sessionFactory"/>
    </property>
</bean>

2.配置事务属性

<tx:advice id="txAdvice" transaction-manager="transactionManager">   
    <tx:attributes>  
        <tx:method name="save*" propagation="REQUIRED" />
	<tx:method name="delete*" propagation="REQUIRED" />
	<tx:method name="update*" propagation="REQUIRED" /> 
	<tx:method name="*" />
    </tx:attributes>  
</tx:advice>

 注:save*、delete*、update*分别代表方法名以save、delete、update开头的方法添加事务管理,

        若配置<tx:method name="*" />则代表所有方法添加事务管理

3.配置事务切入点

<aop:config>
    <aop:pointcut id="serviceOperation" expression="execution(* cn.com.highset.goexpo.bqd.controller.*.*(..))"/>   
    <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>  
</aop:config>

注:execution(* cn.com.highset.goexpo.bqd.controller.*.*(..))中几个通配符的含义如下:

       第一个 * —— 通配任意返回值类型

       第二个 * —— 通配包cn.com.highset.goexpo.bqd.controller下的任意class

       第三个 * —— 通配包cn.com.highset.goexpo.bqd.controller下的任意方法

       第四个 .. —— 通配方法可以有0个或多个参数

       若cn.com.highset.goexpo.bqd.controller包下还有子包,有几级子包添加几个通配符.* 

猜你喜欢

转载自blog.csdn.net/rexueqingchun/article/details/80000269