spring声明式事务的配置

spring声明式事务配置

1、XML配置

(1)配置平台事务管理器
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

 

(2)配置事务通知
<tx:advice id="txAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<!--对方法级别设置事务的隔离级别、传播行为-->
<!--设置了默认的隔离级别-->
<!--添加-->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<!--删除-->
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<!--修改-->
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<!--查询-->
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>

 

(3)配置aop
    <aop:config>
<!-- 这使用的是Spring AOP的实现 -->
<!-- advice-ref:指定advice增强类 -->
<!-- pointcut:指定切点 -->
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* *..*.*ServiceImpl.*(..))" />
</aop:config>

 

2、混合配置

(1)开启事务注解
    <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 配置事务注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

 

(2)用@Transactional注解标注要使用事务的类或者方法

 

3、纯注解配置

@EnableTransactionManagement

 

posted @ 2020-01-20 11:35  第二人生Bonnie  阅读(193)  评论(0编辑  收藏  举报