afterthrowing注解无法捕获异常_简单分享一下手写注解事务的小demo

ef5175c973a07785e31e439feed20869.png

本次创建的工程师maven工程

1、加入依赖,有些依赖是之前其他知识的,可以忽略

org.javassist javassist 3.26.0-GAorg.springframework spring-core 3.0.6.RELEASEorg.springframework spring-context 3.0.6.RELEASEorg.springframework spring-aop 3.0.6.RELEASEorg.springframework spring-orm 3.0.6.RELEASEorg.aspectj aspectjrt 1.6.1aspectj aspectjweaver 1.5.3cglib cglib 2.1_2com.mchange c3p0 0.9.5.2mysql mysql-connector-java 5.1.37dom4j dom4j 1.6.1commons-lang commons-lang 2.6

2、编写注解

@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface ExtTransaction {}

3、编写事务工具类

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Scope;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.stereotype.Component;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.interceptor.DefaultTransactionAttribute;/** * @ClassName TransactionUtils * @Description 事务开启、提交、回滚 * @Author XXX * @Date 2019/12/2 17:40 * @Version 1.0 */@Component@Scope("prototype")public class TransactionUtils { @Autowired private DataSourceTransactionManager dataSourceTransactionManager; private TransactionStatus transactionStatus; public void begin() { System.out.println("开启事务"); transactionStatus = dataSourceTransactionManager.getTransaction(new DefaultTransactionAttribute()); } public void commit() { System.out.println("提交事务"); if (transactionStatus != null) { dataSourceTransactionManager.commit(transactionStatus); } } public void rollback() { System.out.println("事务回滚"); if (transactionStatus != null) { dataSourceTransactionManager.rollback(transactionStatus); } }}

4、利用aop拦截,这里使用了环绕通知和异常通知

import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;import java.lang.reflect.Method;/** * @ClassName AopExtTransaction * @Description TODO * @Author XXX * @Date 2019/12/5 11:03 * @Version 1.0 */@Aspect@Component@Scope("prototype")public class AopExtTransaction { @Autowired private TransactionUtils transactionUtils; @AfterThrowing("execution(* com.service.*.*(..))") public void afterThrowing() { transactionUtils.rollback(); } @Around("execution(* com.service.*.*(..))") public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { //获得该方法上的事务注解 ExtTransaction extTransaction = getMethodExtTransaction(proceedingJoinPoint); //根据注解获得事务并开启 if (extTransaction == null) { proceedingJoinPoint.proceed(); return; } transactionUtils.begin(); //调用目标方法 proceedingJoinPoint.proceed(); //提交事务 transactionUtils.commit(); }/** * 获得方法上的注解 */ private ExtTransaction getMethodExtTransaction(ProceedingJoinPoint proceedingJoinPoint) throws NoSuchMethodException { String methodName = proceedingJoinPoint.getSignature().getName(); Class> classTaget = proceedingJoinPoint.getTarget().getClass(); Class[] par = ((MethodSignature) proceedingJoinPoint.getSignature()).getParameterTypes(); Method objMethod = classTaget.getMethod(methodName, par); ExtTransaction extTransaction = objMethod.getDeclaredAnnotation(ExtTransaction.class); return extTransaction; }}

5、实例

@Servicepublic class UserServiceImpl implements UserService { @Autowired private UserDao userDao;  @ExtTransaction @Override public void add() { userDao.add("zeny", 18);// int i = 1 / 0; System.out.println("添加数据"); userDao.add("zhangsan", 28); }}

注意:不要在上面的实例中捕获异常,否则事务无法正常回滚哦~~~

以上只是简单的实现,如有错漏,敬请原谅~~~有兴趣的小伙伴可以继续优化哦~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值