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

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();}}
}

在这里插入图片描述

项目结构

在这里插入图片描述

http://www.lryc.cn/news/163389.html

相关文章:

  • 9.9校招 实习 内推 面经
  • 互联网医院App开发:构建医疗服务的技术指南
  • 阅读分享--重读Youtube深度学习推荐系统论文,字字珠玑,惊为神文
  • 使用Python操作CSV文件,方便又快捷
  • 深入探索KVM虚拟化技术:全面掌握虚拟机的创建与管理
  • javaee springMVC model的使用
  • Spring与Docker:如何容器化你的Spring应用
  • 试图替代 Python 的下一代AI编程语言:Mojo
  • 【数据结构】栈、队列和数组
  • python算法调用方案
  • 《微服务架构设计模式》第二章
  • taro vue3 ts nut-ui 项目
  • 【群答疑】jmeter关联获取上一个请求返回的字符串,分割后保存到数组,把数组元素依次作为下一个请求的入参...
  • Shell 函数详解(函数定义、函数调用)
  • git-命令行显示当前目录分支
  • pgsql 报错 later table “drop column” is not supported now
  • 如何制定私域流量布局计划?
  • yolov8 模型部署--TensorRT部署-c++服务化部署
  • 自适应迭代扩展卡尔曼滤波算法AIEKF估计SOC VS 扩展卡尔曼估计SOC
  • 2023-亲测有效-git clone失败怎么办?用代理?加git?
  • An Empirical Study of GPT-3 for Few-Shot Knowledge-Based VQA
  • 2023高教社杯数学建模B题思路分析 - 多波束测线问题
  • 02-docker network
  • 栈和队列经典笔试题
  • No5.9:多边形内角和公式
  • EditPlus 配置python 及Anaconda中的python
  • linux 编译 llvm + clang
  • Mybatis 框架 ( 四 ) QueryWrapper
  • 数据结构和算法之二分法查找
  • 系统日期如何在页面展示,框架是react或者vue3