Spring-----声明式事务管理-xml方式配置

一.准备配置文件

1.1 在配置文件中引入新的命名空间 tx

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	**xmlns:tx="http://www.springframework.org/schema/tx"**
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    ">

1.2 在配置文件中配置包扫描,读取配置文件,并配置数据源,配置jdbcTemplate

     <!--配置包扫描-->
    <context:component-scan base-package="com.sxt"/>
    <!--读取配置文件-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--配置jdbcTemolate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg name="dataSource" ref="dataSource"/>
    </bean>

二.配置事务管理器

Spring对事务的支持,必须先配置事务管理器,事务管理器已经封装了事务的具体的处理
DataSourceTransactionManager
使用JDBC,MyBatis的事务管理器;(当前案例使用的Spring的JDBC操作,所以配置这个)

 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--事务管理器里面,需要注入dataSource -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

三.配置事务通知/增强

<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 配置要切的方法 : trans (默认使用的是环绕通知) -->
			<tx:method name="trans"/>
		</tx:attributes>
	</tx:advice>

以上是因为service中只有一个转账的方法,所以可以直接写方法名,但如果方法很对,且都是增删查改的方法,可以做如下配置

  <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="select*" read-only="true"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="query*" read-only="true"/>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>

四.使用AOP将事务通知切入到Service层

 <!-- 3.使用AOP将事务通知切入到Service层-->
    <aop:config>
        <aop:pointcut id="pt" expression="execution(* com.sxt.service..*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>

五.事物方法的属性细节配置

  <tx:method name="select*" read-only="true"/>
  

事务方法属性
name
匹配到的方法模式
read-only
如果为true,开启一个只读事务,只读事务的性能较高,但是不能再只读事务中,使用DML
isolation
代表数据库事务隔离级别(就使用默认)
DEFAULT:
让spring使用数据库默认的事务隔离级别;
no-rollback-for
如果遇到的异常是匹配的异常类型,就不回滚事务
rollback-for
如果遇到的异常是指定匹配的异常类型,才回滚事务;spring默认情况下,spring只会去回滚RuntimeException及其子类,不会回滚Exception和Thowable
propagation
事务的传播方式(当一个方法已经在一个开启的事务当中了,应该怎么处理自身的事务)
1,REQUIRED(默认的传播属性):如果当前方法运行在一个没有事务环境的情况下,则开启一个新的事务,如果当前方法运行在一个已经开启了的事务里面,把自己加入到开启的那个事务中
2,REQUIRES_NEW:不管当前方法是否运行在一个事务空间之内,都要开启自己的事务
timeout
事务处理的超时时间,默认事物管理自动处理 ,可以手动配置

六.事务管理器的分类

在使用spring管理事务的时候,首先得告诉spring使用哪一个事务管理器,使用不同的框架(JdbcTemplate,MyBatis,Hibernate/JPA )使用事务管理器都不同
在这里插入图片描述