尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

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

(42)事务的两种开发模式
📅 发布时间:2026/6/19 19:15:08

事务的全注解式开发

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

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已经起作用了。

相关新闻

  • 2025年谁主沉浮?顶尖重型货架厂家综合评比,重型板材存放架/重型货架超强承重/重型模具货架/伸缩悬臂货架/重型货架重型货架定制厂家哪家强 - 品牌推荐师
  • Compose笔记(六十四)--LottieAnimation
  • HTTP的简介和原理 - saboyou1

最新新闻

  • 终极指南:用Parsec VDD免费扩展你的Windows虚拟显示器
  • 2026年新发布山东靠谱的罐罐酸奶加盟项目深度剖析:为何谷物全书罐罐酸奶成为市场焦点? - 品牌鉴赏官2026
  • 2026清远本地人必选防水补漏检测维修公司靠谱服务商TOP5推荐:房屋渗漏水检测维修/卫生间/厨房/天花板/阳台/外墙渗漏水检测补漏维修-暗管漏水检测专业仪器精准定位漏水点 - 即刻修防水
  • MPC5604P外部中断与DSPI时序参数深度解析与工程实践
  • DFT仿真实战:从STUCK-AT到AT-SPEED的验证要点解析
  • ReadCat安全最佳实践:终极插件安全与用户数据保护指南

日新闻

  • 信任的进化:技术实现详解——如何用JavaScript构建博弈论模拟器
  • Terrakube自定义工作流:如何集成OPA、Infracost等工具扩展IaC能力
  • grunt-concurrent快速入门:5分钟学会并行运行Grunt任务

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号