news 2026/4/23 14:15:45

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
(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已经起作用了。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/22 10:19:09

YOLO在智慧交通中的落地实践:GPU集群调度方案

YOLO在智慧交通中的落地实践&#xff1a;GPU集群调度方案 在城市主干道的早高峰时段&#xff0c;数百个路口摄像头正源源不断地将高清视频流上传至数据中心。每一帧画面中都可能隐藏着一辆闯红灯的车辆、一个横穿马路的行人&#xff0c;或是一起刚刚发生的交通事故。传统系统往…

作者头像 李华
网站建设 2026/4/22 13:55:40

YOLO模型支持Logstash日志处理管道

YOLO模型支持Logstash日志处理管道 在智能制造与边缘AI部署日益深入的今天&#xff0c;一个常被忽视的问题浮出水面&#xff1a;我们能让AI“看见”世界&#xff0c;却难以让系统真正“记住”它所见的内容。 摄像头每秒生成成百上千帧画面&#xff0c;YOLO等目标检测模型能实时…

作者头像 李华
网站建设 2026/4/20 20:47:04

YOLOv9-Slim轻量版发布:移动端推理速度再提升

YOLOv9-Slim轻量版发布&#xff1a;移动端推理速度再提升 在智能手机、无人机和工业相机日益普及的今天&#xff0c;实时目标检测已不再是云端服务器的专属能力。越来越多的应用场景要求设备“看得懂”世界——从自动避障的扫地机器人&#xff0c;到产线上毫秒级响应的异物识别…

作者头像 李华
网站建设 2026/4/23 6:33:08

YOLO目标检测支持STOMP协议WebSocket消息

YOLO目标检测与STOMP over WebSocket的融合实践 在现代智能视觉系统中&#xff0c;一个日益突出的挑战是&#xff1a;如何将强大的感知能力真正“接入”到业务流程之中。许多项目并非败于模型精度不足&#xff0c;而是卡在了从“看得见”到“传得快、接得住”的最后一公里——…

作者头像 李华
网站建设 2026/4/23 13:09:13

基于SpringBoot的宠物成长监管系统的设计与实现(源码+文档+部署+讲解)

本课题聚焦宠物主人对宠物成长全周期监管的需求痛点&#xff0c;设计并实现一款基于SpringBoot框架的宠物成长监管系统&#xff0c;解决传统宠物养护中成长记录零散、健康数据缺失、养护知识匮乏、就医预约不便等问题&#xff0c;搭建一体化宠物成长数字化监管平台。系统采用前…

作者头像 李华
网站建设 2026/4/18 14:47:46

YOLO模型训练支持Multi-scale Training提升鲁棒性

YOLO模型训练支持Multi-scale Training提升鲁棒性 在工业质检线上&#xff0c;一台摄像头需要同时识别远处模糊的微小划痕和近处清晰的大型部件&#xff1b;在高速公路上&#xff0c;自动驾驶系统必须稳定追踪从几百米外驶来的车辆&#xff0c;直到它逼近到眼前。这些场景背后隐…

作者头像 李华