news 2026/3/28 17:28:30

(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/3/28 6:43:56

YOLOv8-Detect-V8改进版:主干网络再优化

YOLOv8-Detect-V8改进版&#xff1a;主干网络再优化 在工业质检线上&#xff0c;一个微小的电子元件缺失可能意味着整批产品返工&#xff1b;在高速公路上&#xff0c;自动驾驶系统对远处车辆的漏检可能带来严重后果。这些现实场景不断向目标检测算法提出更严苛的要求——既要看…

作者头像 李华
网站建设 2026/3/23 10:37:26

YOLO目标检测准确率提升技巧:混合精度训练+GPU支持

YOLO目标检测准确率提升技巧&#xff1a;混合精度训练GPU支持 在工业自动化、智能安防和自动驾驶等场景中&#xff0c;实时目标检测早已不再是“能不能做”的问题&#xff0c;而是“能不能快且准地做”的工程挑战。面对海量图像数据和严苛的响应延迟要求&#xff0c;YOLO&#…

作者头像 李华
网站建设 2026/3/28 8:11:21

YOLO训练时GPU显存爆了?常见问题与解决方案汇总

YOLO训练时GPU显存爆了&#xff1f;常见问题与解决方案汇总 在部署一个实时缺陷检测系统时&#xff0c;工程师小李信心满满地启动YOLOv8的训练脚本&#xff0c;结果几秒后终端弹出熟悉的红色错误&#xff1a; CUDA out of memory. Tried to allocate 256.00 MiB...这不是个例。…

作者头像 李华
网站建设 2026/3/26 21:43:17

YOLO目标检测准确率低?可能是数据和GPU协同出了问题

YOLO目标检测准确率低&#xff1f;可能是数据和GPU协同出了问题 在工业质检线上&#xff0c;一张高清图像从相机捕获到缺陷判定本应只需几毫秒。但现实往往是&#xff1a;模型推理明明很快&#xff0c;整体延迟却居高不下&#xff1b;训练日志显示loss震荡剧烈&#xff0c;最终…

作者头像 李华
网站建设 2026/3/27 0:31:35

YOLO模型量化压缩后,还能在低端GPU上跑出高性能吗?

YOLO模型量化压缩后&#xff0c;还能在低端GPU上跑出高性能吗&#xff1f; 在智能摄像头遍布工厂车间、无人机巡检输电线路、车载系统实时识别交通标志的今天&#xff0c;目标检测早已不再是实验室里的炫技项目。它正以惊人的速度渗透进我们生活的每一个角落——而支撑这一切的…

作者头像 李华