新建工程

把对应的依赖复制过来

src下内容复制

找到bean.xml开始配置

里面需要注入DataSource

<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"
    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">这样就都导入进来了。
配置事物的通知需要用到标签 tx:advice

id是唯一标识,属性就叫做transaction-manager

这里可以直接选择上面引用的



现在有了通知,通知里面有提交和回滚的方法

他们两个需要建立关系才能对方法进行增强

使用aop:advisor这个标签



transfer是我们业务层接口中的一个方法,它有一些实现

有这些属性


星是个通配符。上面的全通配,下面的部分能匹配。

优先级,下面的要高于上面

当前实现类里面有异常

数据改回两个一千

运行测试方法


事务被控制住了。金额没有发生改变。

当用了这个切入点表达式,我们的项目中这个配置写一次。项目中再也没有实物的问题干扰我们了。
这就是声明式事务的好处

阶段3 2.Spring_10.Spring中事务控制_6 spring基于XML的声明式事务控制-配置步骤
原文:https://www.cnblogs.com/wangjunwei/p/11370132.html