基于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>
复制代码

 

posted @   kkzhang  阅读(507)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示