1.编程式事务控制相关对象
2.
事务的隔离级别
事务的传播行为
3.
2. 基于XML的声明式事务
2.1 什么是声明式事务
Spring的声明式事务,顾名思义就是采用声明的方式来处理事务,这里说的在配置文件中声明,用在spring配置文件中声明式的处理事务代替代码式处理事务。
作用:事务不侵入开发的组件,具体来说,业务逻辑对象就不会意识到正在事务管理当中,事实上也应该如此,因为事务管理属于系统层面的服务,而不是业务逻辑的一部分,如果想要改变事务管理策划的话,只需要在配置文件中修改即可。
在不需要事务管理的时候,只要在设定文件上修改一下,即可移去事务管理服务,无需改变代码,重新编译,这样维护起来方便。
注意:spring声明式事务控制底层就是aop。
2.2 实现
谁是切点? 切点就是需要实现事务的业务方法
谁是通知? 就是事务
配置切面
//此时我们需要导入事务的约束:tx名称和空间约束,同时也需要aop的
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
//配置平台事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTranscationManager">
<property name="datasource" ref="datasource"/>
</bean>
// 使用tx:advice标签配置事务通知
//属性:
//id:给事务通知起一个唯一标识。
//transaction-manager:给事务通知提供一个事务管理器引用。
<!--配置事务的通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
//<!--配置AOP-->
<aop:config>
<!--配置切入点表达式-->
<aop:pointcut id="pt01" expression="execution(* net.togogo.service.impl.*.*(..))"/>
<!--建立切入点表达式和事务通知的对应关系-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt01"></aop:advisor>
</aop:config>
3.基于注解的声明式事务控制