当前位置: 首页 > news >正文

(42)事务的两种开发模式

事务的全注解式开发

编写一个类来代替配置文件,代码如下:

packagecom.powernode.bank;importcom.alibaba.druid.pool.DruidDataSource;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.ComponentScan;importorg.springframework.context.annotation.Configuration;importorg.springframework.jdbc.core.JdbcTemplate;importorg.springframework.jdbc.datasource.DataSourceTransactionManager;importorg.springframework.transaction.annotation.EnableTransactionManagement;importjavax.sql.DataSource;/** * @author 动力节点 * @version 1.0 * @className Spring6Config * @since 1.0 **/@Configuration@ComponentScan("com.powernode.bank")@EnableTransactionManagementpublicclassSpring6Config{@BeanpublicDataSourcegetDataSource(){DruidDataSourcedataSource=newDruidDataSource();dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/spring6");dataSource.setUsername("root");dataSource.setPassword("root");returndataSource;}@Bean(name="jdbcTemplate")publicJdbcTemplategetJdbcTemplate(DataSourcedataSource){JdbcTemplatejdbcTemplate=newJdbcTemplate();jdbcTemplate.setDataSource(dataSource);returnjdbcTemplate;}@BeanpublicDataSourceTransactionManagergetDataSourceTransactionManager(DataSourcedataSource){DataSourceTransactionManagerdataSourceTransactionManager=newDataSourceTransactionManager();dataSourceTransactionManager.setDataSource(dataSource);returndataSourceTransactionManager;}}

测试程序如下:

@TestpublicvoidtestNoXml(){ApplicationContextapplicationContext=newAnnotationConfigApplicationContext(Spring6Config.class);AccountServiceaccountService=applicationContext.getBean("accountService",AccountService.class);try{accountService.transfer("act-001","act-002",10000);System.out.println("转账成功");}catch(Exceptione){e.printStackTrace();}}

执行结果:

数据库表中数据:

声明式事务之XML实现方式

配置步骤:

  • 第一步:配置事务管理器
  • 第二步:配置通知
  • 第三步:配置切面

记得添加aspectj的依赖:

<!--aspectj依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>6.0.0-M2</version></dependency>

Spring配置文件如下:
记得添加aop的命名空间。

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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-scanbase-package="com.powernode.bank"/><beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"><propertyname="driverClassName"value="com.mysql.cj.jdbc.Driver"/><propertyname="url"value="jdbc:mysql://localhost:3306/spring6"/><propertyname="username"value="root"/><propertyname="password"value="root"/></bean><beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate"><propertyname="dataSource"ref="dataSource"/></bean><!--配置事务管理器--><beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"/></bean><!--配置通知--><tx:adviceid="txAdvice"transaction-manager="txManager"><tx:attributes><tx:methodname="save*"propagation="REQUIRED"rollback-for="java.lang.Throwable"/><tx:methodname="del*"propagation="REQUIRED"rollback-for="java.lang.Throwable"/><tx:methodname="update*"propagation="REQUIRED"rollback-for="java.lang.Throwable"/><tx:methodname="transfer*"propagation="REQUIRED"rollback-for="java.lang.Throwable"/></tx:attributes></tx:advice><!--配置切面--><aop:config><aop:pointcutid="txPointcut"expression="execution(* com.powernode.bank.service..*(..))"/><!--切面 = 通知 + 切点--><aop:advisoradvice-ref="txAdvice"pointcut-ref="txPointcut"/></aop:config></beans>

将AccountServiceImpl类上的@Transactional注解删除。
编写测试程序:

@TestpublicvoidtestTransferXml(){ApplicationContextapplicationContext=newClassPathXmlApplicationContext("spring2.xml");AccountServiceaccountService=applicationContext.getBean("accountService",AccountService.class);try{accountService.transfer("act-001","act-002",10000);System.out.println("转账成功");}catch(Exceptione){e.printStackTrace();}}

执行结果:

数据库表中记录:

通过测试可以看到配置XML已经起作用了。

http://www.jsqmd.com/news/154253/

相关文章:

  • 生成式AI移动应用测试工程方法论
  • 2025白刚玉定制口碑前十强,你选对了吗?磨料/金刚砂/碳化硅/白刚玉/不锈钢灰/精密铸造砂/棕刚玉/铬刚玉/黑碳化硅白刚玉厂家有哪些 - 品牌推荐师
  • YOLO + Triton推理服务器:构建高并发检测服务
  • 2025年谁主沉浮?顶尖重型货架厂家综合评比,重型板材存放架/重型货架超强承重/重型模具货架/伸缩悬臂货架/重型货架重型货架定制厂家哪家强 - 品牌推荐师
  • Compose笔记(六十四)--LottieAnimation
  • HTTP的简介和原理 - saboyou1
  • 2025.12.28博客
  • 智能 Agent 的概念建模与系统特征分析-从传统程序到自主智能体的范式演进
  • vue 甘特图 vxe-gantt table 可视化依赖线的使用,可视化拖拽创建连接线的用法
  • Cloudflare Wrangler CLI 备忘
  • 论文阅读(十二月第四周)
  • 做不出IT毕设,我是废物吗?
  • YOLO模型支持Logstash日志处理管道
  • YOLO模型训练支持CutOut与HideAndSeek图像遮挡增强
  • 2025年三合一过滤洗涤干燥机厂家排名:哈氏合金/搪瓷设备专业制造商推荐 - mypinpai
  • 从“拔网线“看互联网大厂的安全攻防:黑灰产攻击原理与防御实战
  • Next AI Draw.io 核心实现深度分析
  • HTTP的原理 - saboyou1
  • 2025年优质尼龙拖链品牌排行榜,尼龙拖链与其他材质拖链对比优势在哪? - mypinpai
  • 12/28-正式1级打卡题1(课后)
  • YOLO目标检测准确率提升技巧:混合精度训练+GPU支持
  • YOLOv9-SPPF结构详解:为何它更适合边缘GPU设备?
  • 鸿蒙原生系列之动画效果(关键帧动画)
  • 2025郑州西餐培训推荐TOP5权威榜单:甄选靠谱机构避坑指南,新手入门必看 - myqiye
  • YOLO训练日志分析:如何通过GPU利用率诊断瓶颈
  • 使用YOLO做实时检测,你真的选对GPU了吗?
  • YOLO开源项目推荐:这10个高星项目都用了我们的GPU
  • YOLO在冰川变化监测中的应用:遥感图像分析实践
  • 2025黑龙江大理石瓷砖品牌TOP5权威推荐:客厅阳台效果+日常保养全指南 - 工业推荐榜
  • 2025年度哈尔滨卫生间瓷砖行业口碑品牌排行榜 - 工业品牌热点