• 基于XML文档的声明式事务配置


    无论是使用注解的方式还是XML,最终支持事务管理的一定是事务管理器dataSourceTransactionManager,事务管理是AOP中的一个简单应用,AOP面向切面编程,切面就是从目标对象中抽取出来一些公共功能或一些非核心业务代码所存放的类,面向对象为纵向继承,面向切面为横向抽取。

    1、创建切面;2、写通知;3、写切入点表达式。事务管理器就相当于AOP中的切面,如果在XML中就要配置切入点表达式。

    需要通过XML配置事务:

    1、配置事务管理器DataSourceTransactionManager (依赖于数据源)

    2、写切入点表达式(通知作用到连接点上) aop:pointcut

    3、配置事务通知tx:advice,(依赖事务管理器), 在设置好的切入点表达式下再次进行事务设置,选择加事务的方法名tx:method

    4、切入点和通知要联系起来<aop:advisor advice-ref="tx" pointcut-ref="pointCut"/>。

    <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        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-4.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    
        <context:component-scan base-package="com.atguigu.book_xml"></context:component-scan>
        
        <!-- 引入属性文件 -->
        <context:property-placeholder location="db.properties"/>
    
        <!-- 创建数据源 -->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${jdbc.driver}"></property>
            <property name="url" value="${jdbc.url}"></property>
            <property name="username" value="${jdbc.username}"></property>
            <property name="password" value="${jdbc.password}"></property>
        </bean>
    
        <!-- 通过数据源配置JdbcTemplate -->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <!-- 配置事务管理器,不管是用注解方式或xml方式配置事务,一定要有DataSourceTransactionManager事务管理器的支持 
            事务依赖于数据源所产生的连接对象,只有连接对象创建成功了才能够处理事务
        -->
        <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <!-- 配置事务通知,依赖于事务管理器 -->
        <tx:advice id="tx" transaction-manager="dataSourceTransactionManager">
            <tx:attributes>
                <!-- 在设置好的切入点表达式下再次进行事务设置 -->
                <tx:method name="buyBook"/>
                <tx:method name="checkOut"/>
                
                <!-- 只有select开头的方法才会被事务处理 ,利用通配符-->
                <tx:method name="select*" read-only="true"/>
                <tx:method name="insert*"/>
                <tx:method name="update*"/>
                <tx:method name="delete*"/>
                
                <tx:method name="*"/>
                
            </tx:attributes>
        </tx:advice>
    
        <!-- 配置切入点表达式,一定是针对于切面如何作用于连接点上。需要将切入点表达式和事务通知联系起来 -->
        <aop:config>
            <aop:pointcut expression="execution(* com.atguigu.book_xml.service.impl.*.*(..))" id="pointCut"/>
            <aop:advisor advice-ref="tx" pointcut-ref="pointCut"/>
        </aop:config>
        
    </beans>
  • 相关阅读:
    crash reporting system for Windows applications
    1
    qt 试用 (3)配置编译源代码及调试
    kd tree & ray tracing
    new
    KMP算法中关于next数组的探究
    teamviewer vs echovnc
    NAT之stun确定nat类型
    Wireshark
    GNU httptunnel,当SSH被block时的选择
  • 原文地址:https://www.cnblogs.com/lemonzhang/p/12917795.html
Copyright © 2020-2023  润新知