(VI)事务:Spring 事务-XML配置

一、基于声明式事务

1、Spring中提供事务管理器(事务切面),配置这个事务管理器

2、开启基于注解的事务式事务,依赖 tx 名称空间

3、给事务方法加注解

二、基于xml的事务控制

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/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">

    <!--扫描组件-->
    <context:component-scan base-package="com.njf.tx"></context:component-scan>

    <!--导入数据库配置文件-->
    <context:property-placeholder location="db.properties"></context:property-placeholder>

    <!--  配置数据源  -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
    </bean>

    <!--创建 jdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--
        配置声明式事务
            1、Spring中提供事务管理器(事务切面),配置这个事务管理器
            2、开启基于注解的事务式事务,依赖 tx 名称空间
            3、给事务方法加注解
    -->

    <!--
        基于xml配置的事务
            1、Spring中提供事务管理器(事务切面),配置这个事务管理器
            2、配置出事务方法
            3、告诉Spring哪些方法是事务方法(事务切面按照我们的切入点表达式去切入事务方法)
    -->
    <!--1: 配置事务管理器让其进行事务控制-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--控制住数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* com.njf.tx.ser*.*.*(..))"/>
        <!--  事务建议,事务增强  advice-ref:指向事务管理器的配置-->
        <aop:advisor advice-ref="myAdvice" pointcut-ref="txPointcut"></aop:advisor>
    </aop:config>

    <!--  配置事务管理器: transaction-manager="transactionManager"指定是配置哪个事务管理器 -->
    <tx:advice id="myAdvice" transaction-manager="transactionManager">
        <!--事务属性-->
        <tx:attributes>
            <!-- 指定哪些方法是事务方法 切入点表达式只是说,事务管理器要切入这些方法,哪些方法要加事务,使用tx:method 指定-->
            <tx:method name="*"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="checkOut" />

            <!-- 在设置好的切入点表达式下再次进行事务设置 -->
            <tx:method name="buyBook"/>

            <!-- 只有select开头的方法才会被事务处理 -->
            <tx:method name="select*" read-only="true"/>
            <tx:method name="insert*"/>
            <tx:method name="update*"/>
            <tx:method name="delete*"/>

            <!-- 设置具体方法的事务属性 -->
            <tx:method name="purchase"
                       isolation="READ_COMMITTED"
                       no-rollback-for="java.lang.ArithmeticException,java.lang.NullPointerException"
                       propagation="REQUIRES_NEW"
                       read-only="false"
                       timeout="10"/>

            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

</beans>
原文地址:https://www.cnblogs.com/niujifei/p/15505802.html