1.首先自定义一个事务注解(类似于@Transactional)
package com.itmayiedu.annotation;
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; //事务注解 设置传播行为 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public ExtTransaction { } 2.编写切面类AopExtTransaction.javapackage com.itmayiedu.aop;
import java.lang.annotation.Annotation; import java.lang.reflect.Method; 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.stereotype.Component; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.interceptor.TransactionAspectSupport; import com.itmayiedu.annotation.ExtTransaction; import com.itmayiedu.transaction.TransactionUtils; @Aspect//自定义事务注解具体实现 public class AopExtTransaction { @Autowired// 一个事务实例 针对一个事务,所以要在TransactionUtils上加Scope(value="prototype") private TransactionUtils transactionUtils; @AfterThrowing("execution(* com.itmayiedu.service.*.*.*(..))") public void afterThrowing(){ // 获取当前事务进行回滚 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); } @Around("execution(* com.itmayiedu.service.*.*.*(..))") public void around(ProceedingJoinPoint pjp) throws Throwable{ // 1.获取该方法上是否加上注解 ExtTransaction extTransaction = getMethodExtTransaction(pjp); TransactionStatus transactionStatus = begin(extTransaction); // 2.调用目标代理对象方法 pjp.proceed(); commit(transactionStatus); }private void commit(TransactionStatus transactionStatus) {
// 5.如果存在注解,提交事务 if(transactionStatus!=null){ transactionUtils.commit(transactionStatus); } }private TransactionStatus begin(ExtTransaction extTransaction) {
if(extTransaction==null){ return null; } // 2.如果存在事务注解,开启事务 return transactionUtils.begin(); } // 获取方法上是否存在事务注解 private ExtTransaction getMethodExtTransaction(ProceedingJoinPoint pjp) throws NoSuchMethodException, SecurityException { String methodName = pjp.getSignature().getName(); // 获取目标对象 Class<?> classTarget = pjp.getTarget().getClass(); // 获取目标对象类型 Class<?>[] par = ((MethodSignature) pjp.getSignature()).getParameterTypes(); // 获取目标对象方法 Method objMethod = classTarget.getMethod(methodName, par); Annotation[] declaredAnnotations = objMethod.getDeclaredAnnotations(); ExtTransaction extTransaction=(ExtTransaction) declaredAnnotations[0]; return extTransaction; }}
3.UserServiceImpl.javapackage com.itmayiedu.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.interceptor.TransactionAspectSupport;import com.itmayiedu.annotation.ExtTransaction;
import com.itmayiedu.dao.UserDao; import com.itmayiedu.service.UserService; import com.itmayiedu.transaction.TransactionUtils; @Service public class UserServiceImpl implements UserService{ @Autowired private UserDao userDao; @Autowired private TransactionUtils transactionUtils; @ExtTransaction public void add() { // 调用接口的时候 接口失败 需要回滚,但是日志记录不需要回滚。 //logService.addLog(); // 后面程序发生错误,不能影响到我的回滚### 正常当addLog方法执行完毕,就应该提交事务 userDao.add("test001", 20); int i = 1 / 0; System.out.println("################"); userDao.add("test002", 21); } } 4.编写测试类Test01.javapackage com.itmayiedu.service;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test01 { public static void main(String[] args) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); UserService userService = (UserService) applicationContext.getBean("userServiceImpl"); userService.add(); } } 解析:在走useService.add()之前被aop拦截,先走环绕通知(开启事务,提交事务),如果useService.add()里面有异常就走异常通知(回滚事务)