javaee spring aop实现事务 项目结构
spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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"><!-- 1.开启 注解 --><context:component-scan base-package="com.test" /><!-- 2.创建数据源对象--><context:property-placeholder location="db.properties" /><bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${driverClass}" /><property name="jdbcUrl" value="${url}" /><property name="user" value="${user}" /><property name="password" value="${password}" /></bean><!-- 3.创建JdbcTemplate对象--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><constructor-arg name="dataSource" ref="comboPooledDataSource" /></bean><!-- 4 创建一个事务管理器 --><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="comboPooledDataSource" /></bean><!-- 5.定义事务方法 (增强代码) --><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="transfer" isolation="DEFAULT"propagation="REQUIRED" rollback-for="MyException"/></tx:attributes></tx:advice><!-- 6.织入 --><aop:config><!-- 定义切点--><aop:pointcut id="pc" expression="execution(* com..CardInfoService.transfer(..))"></aop:pointcut><!-- 织入--><aop:advisor advice-ref="txAdvice" pointcut-ref="pc" /></aop:config></beans>
实现类
package com.test.service.impl;import com.test.dao.ICardInfoDao;
import com.test.exception.MyException;
import com.test.service.ICardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class CardInfoService implements ICardInfoService {@Autowiredprivate ICardInfoDao cardInfoDao;public ICardInfoDao getCardInfoDao() {return cardInfoDao;}public void setCardInfoDao(ICardInfoDao cardInfoDao) {this.cardInfoDao = cardInfoDao;}//实现转账方法@Overridepublic void transfer(int from, int to, float money) throws Exception {//一方减钱cardInfoDao.decreaseMoney(from,money);if(true)throw new MyException("转账异常");//一方加钱cardInfoDao.increaseMoney(to,money);}
}
异常类
package com.test.exception;//模拟一个异常类
public class MyException extends Exception {public MyException(){super();}public MyException(String message){super(message);}
}
pom依赖
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>testSpring11</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>testSpring11 Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- 导入spring的核心jar包 --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.3.18.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.3.18.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.18.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.3.18.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>4.3.18.RELEASE</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.37</version></dependency><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><!-- 配置的 spring-jdbc --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.3.18.RELEASE</version></dependency><!-- 事务需要的jar包--><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>4.3.18.RELEASE</version></dependency><!--导入aop相关的jar包--><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>4.3.18.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.3.18.RELEASE</version></dependency></dependencies><build><finalName>testSpring11</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>
</project>
测试类
package com.test.service;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestCardInfoService {@Testpublic void test(){ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");ICardInfoService cardInfoServiceProxy= applicationContext.getBean("cardInfoService",ICardInfoService.class);try {cardInfoServiceProxy.transfer(2,1,1000);} catch (Exception e) {e.printStackTrace();}}
}

项目结构

相关文章:
javaee spring aop实现事务 项目结构
spring配置文件 <?xml version"1.0" encoding"UTF-8"?> <beans xmlns"http://www.springframework.org/schema/beans"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xmlns:context"http://www.springframewo…...
9.9校招 实习 内推 面经
绿泡*泡: neituijunsir 交流裙 ,内推/实习/校招汇总表格 1、自动驾驶一周资讯 -理想汽车计划进军自动驾驶卡车领域,宝马联合亚马逊开发下一代自动驾驶平台,丰田汽车重组自动驾驶和人工智能子公司 自动驾驶一周资讯 -理想汽车…...
互联网医院App开发:构建医疗服务的技术指南
互联网医院App的开发是一个复杂而具有挑战性的任务,但它也是一个充满潜力的领域,可以为患者和医疗专业人员提供更便捷的医疗服务。本文将引导您通过一些常见的技术步骤来构建一个简单的互联网医院App原型,以了解该过程的基本概念。 技术栈选…...
阅读分享--重读Youtube深度学习推荐系统论文,字字珠玑,惊为神文
重读Youtube深度学习推荐系统论文,字字珠玑,惊为神文 https://zhuanlan.zhihu.com/p/52169807 废话不多说,下面就跟大家分享一下两次拜读这篇论文的不同体验和收获。 第一遍读这篇论文的时候,我想所有人都是冲着算法的架构去的,在深度学习推荐系统已经成为各大公司“基本…...
使用Python操作CSV文件,方便又快捷
概念 CSV是逗号分隔值或者字符分割值,其文件以纯文本形式存储表格数据。 CSV文件可以用文本文件或者转换成EXCEL(直接用EXCEL也可以,但是可能会有一些问题)打开。因此更适合通过CSV文件进行程序之间转移表格数据。 应用场景 需…...
深入探索KVM虚拟化技术:全面掌握虚拟机的创建与管理
文章目录 安装KVM开启cpu虚拟化安装KVM检查环境是否正常 KVM图形化创建虚拟机上传ISO创建虚拟机加载镜像配置内存添加磁盘能否手工指定存储路径呢?创建成功安装完成查看虚拟机 KVM命令行创建虚拟机创建磁盘通过命令行创建虚拟机手动安装虚拟机 KVM命令行创建虚拟机-…...
javaee springMVC model的使用
项目结构图 pom依赖 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://maven.apache.org…...
Spring与Docker:如何容器化你的Spring应用
🌷🍁 博主猫头虎(🐅🐾)带您 Go to New World✨🍁 🦄 博客首页——🐅🐾猫头虎的博客🎐 🐳 《面试题大全专栏》 🦕 文章图文…...
试图替代 Python 的下一代AI编程语言:Mojo
文章目录 为什么叫 Mojo ?Python 家族的一员,MojoPython 的好处:Python 兼容性Python 的问题移动和服务器部署:Python 子集和其他类似 Python 的语言: Mojo 是一种创新的编程语言,结合了 Python 的可用性和…...
【数据结构】栈、队列和数组
栈、队列和数组 栈队列数组数组的顺序表示和实现顺序表中查找和修改数组元素 矩阵的压缩存储特殊矩阵稀疏矩阵 栈 初始化 #define MaxSize 50//栈中元素的最大个数 typedef char ElemType;//数据结构 typedef struct{int top;//栈顶指针ElemType data[MaxSize];//存放栈中的元…...
python算法调用方案
1、python算法部署方案 (1)独立部署 算法端和应用端各自独立部署。 使用WSGI(flask)web应用A包装算法,并发布该应用A。 应用端B 通过httpclient调用算法应用A中的api接口。 (2)统一部署 算法…...
《微服务架构设计模式》第二章
文章目录 微服务架构是什么软件架构是什么软件架构的定义软件架构的41视图模型为什么架构如此重要 什么是架构风格分层式架构风格六边形架构风格微服务架构风格什么是服务什么是松耦合共享类库的角色 为应用程序定义微服务架构识别操作系统根据业务能力进行拆分业务能力定义了一…...
taro vue3 ts nut-ui 项目
# 使用 npm 安装 CLI $ npm install -g tarojs/cli 查看 Taro 全部版本信息 可以使用 npm info 查看 Taro 版本信息,在这里你可以看到当前最新版本 npm info tarojs/cli 项目初始化 使用命令创建模板项目: taro init 项目名 taro init myApp …...
【群答疑】jmeter关联获取上一个请求返回的字符串,分割后保存到数组,把数组元素依次作为下一个请求的入参...
一个非常不错的问题,来检验下自己jmeter基本功 可能有同学没看懂题,这里再解释一下,上面问题需求是:jmeter关联获取上一个请求返回的字符串,分割后保存到数组,把数组元素依次作为下一个请求的入参 建议先自…...
Shell 函数详解(函数定义、函数调用)
Shell 函数的本质是一段可以重复使用的脚本代码,这段代码被提前编写好了,放在了指定的位置,使用时直接调取即可。 Shell 中的函数和C、Java、Python、C# 等其它编程语言中的函数类似,只是在语法细节有所差别。 Shell 函数定义的语…...
git-命令行显示当前目录分支
1. 打开家目录.bashrc隐藏文件,找到如下内容 forlinxubuntu:~$ vi ~/.bashrcif [ "$color_prompt" yes ]; thenPS1${debian_chroot:($debian_chroot)}\[\033[01;32m\]\u\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ elsePS1${debian_chroot:($debi…...
pgsql 报错 later table “drop column” is not supported now
报错 使用pgsql执行下面的SQL报错 alter table test_user drop clolumn name;报错信息: later table “drop column” is not supported now。 报错原因 hologres pgsql的数据库: 删除列目前还是灰度测试阶段,需要在sql前加上set hg_ex…...
如何制定私域流量布局计划?
01 确定目标用户群体 首先,明确目标用户是私域流量布局的基础。可以通过市场调研、用户画像和数据分析等方式,了解目标用户的年龄、性别、兴趣爱好等特征,为后续精准营销奠定基础。 02 选择合适的私域流量渠道 根据目标用户群体的特点&…...
yolov8 模型部署--TensorRT部署-c++服务化部署
写目录 yolov8 模型部署--TensorRT部署1、模型导出为onnx格式2、模型onnx格式转engine 部署 yolov8 模型部署–TensorRT部署 1、模型导出为onnx格式 如果要用TensorRT部署YOLOv8,需要先使用下面的命令将模型导出为onnx格式: yolo export modelyolov8n.p…...
自适应迭代扩展卡尔曼滤波算法AIEKF估计SOC VS 扩展卡尔曼估计SOC
自适应迭代扩展卡尔曼滤波算法(AIEK) 自适应迭代扩展卡尔曼滤波算法(AIEK)是一种滤波算法,其目的是通过迭代过程来逐渐适应不同的状态和环境,从而优化滤波效果。 该算法的基本思路是在每一步迭代过程中&a…...
电子电路设计中7种关键接口技术解析与应用
1. 电路接口概述:信号传输的关键桥梁在嵌入式系统和电子电路设计中,接口技术就像城市之间的高速公路系统。当CPU需要与传感器"对话",当存储器要与处理器"交换情报",这些不同模块之间的信号传输总会面临三大挑…...
Go - Zerolog使用入门
特点高性能:零分配设计,极高的写入速度,对 GC 几乎无压力。结构化日志:默认输出 JSON 格式,便于日志系统(如 ELK、Loki)解析和检索。支持 context:可以在请求链路中传递和追加日志字…...
I2C总线原理与嵌入式系统应用实践
1. I2C总线基础解析I2C(Inter-Integrated Circuit)总线是Philips半导体(现NXP)在1982年推出的双线制串行通信协议。作为一名电子工程师,我在多个嵌入式项目中都深度使用过这种总线。它的精妙之处在于仅用两根线&#x…...
房屋租赁管理系统开发教程:基于SSM框架实战全记录
房屋租赁管理系统 java项目ssm框架开发,全套视频教程Verio 房屋租赁系统“我的收藏”功能深度解析——从用户点击到数据落地的全流程设计一、业务定位在房屋租赁平台中,“收藏”是连接「浏览」与「决策」的关键节点。Verio 把收藏做成一个轻量级、可复用的“微服务”…...
网站主机技术概述
网站主机技术概述 随着互联网技术的飞速发展,网站已经成为企业和个人展示形象、提供服务的必要平台。网站主机的选择对于网站的稳定性和访问速度至关重要。本文将详细阐述网站主机技术,包括其基本概念、类型、选择标准以及未来发展趋势。 一、网站主机基本概念 网站主机,…...
2026届最火的AI论文助手推荐榜单
Ai论文网站排名(开题报告、文献综述、降aigc率、降重综合对比) TOP1. 千笔AI TOP2. aipasspaper TOP3. 清北论文 TOP4. 豆包 TOP5. kimi TOP6. deepseek 要想切实有效地把文本的AIGC检测概率给降低下去,就得从词汇多样性、句式结构以及…...
在华为OpenEuler上同时安装Python 3.8.6和3.9.0,我是如何解决依赖冲突和whl包不全问题的
在华为OpenEuler上实现Python 3.8.6与3.9.0双版本共存的实战指南 当开发环境需要同时支持Python 3.8.6和3.9.0时,许多开发者都会面临依赖冲突、whl包不兼容等问题。特别是在华为OpenEuler这样的企业级操作系统上,系统自带的Python版本可能无法满足特定项…...
Ollama环境变量全解析:除了OLLAMA_GPU_LAYER,这些参数也能大幅提升你的模型运行效率
Ollama环境变量全解析:除了OLLAMA_GPU_LAYER,这些参数也能大幅提升你的模型运行效率 当你已经成功配置Ollama的GPU基础功能后,真正的性能优化之旅才刚刚开始。那些隐藏在环境变量列表中的参数,就像赛车引擎舱内的精密调校旋钮&…...
实战应用:基于快马平台构建企业级msi安装解决方案,涵盖检测、安装与配置
实战应用:基于快马平台构建企业级msi安装解决方案 最近在帮公司优化软件发布流程时,遇到了一个典型问题:如何确保我们的软件产品能够稳定、可靠地部署到客户环境中。特别是当涉及到复杂的依赖项检查和系统配置时,手动安装不仅效率…...
烟花算法(FWA)在优化问题中的实战:与PSO、遗传算法对比,我该选哪个?
烟花算法(FWA)在优化问题中的实战:与PSO、遗传算法对比,我该选哪个? 当面对复杂的优化问题时,算法工程师常常陷入选择困境:粒子群优化(PSO)的快速收敛、遗传算法(GA)的全局搜索能力,还是新兴的烟花算法(FWA…...
