文章目录
-
-
- TxAroundAdvice.java
- [简化了前面 4种(@Before、@AfterReturning、@AfterThrowing、@After)](#简化了前面 4种(@Before、@AfterReturning、@AfterThrowing、@After))
-
在 spring-aop 的 基础使用 -3 - 切点表达式 的提取、复用
的基础上,在看这个文章

TxAroundAdvice.java
java
package com.english.advice;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
/**
* 环绕通知 @Around,
* 对应整个 try...catch...finally 结构,
* 它包括,前面4种通知的所有功能,
* 前面4种通知:
* 前置 @Before
* 后置 @AfterReturning
* 异常 @AfterThrowing
* 最后 @After
*/
@Aspect
@Component
public class TxAroundAdvice {
// 这里的 ProceedingJoinPoint joinPoint 比前面说到的,获取目标方法的信息JoinPoint joinPoint,多了一个触发目标方法执行的方法proceed()
@Around("com.english.pointcut.MyPointCut.mypc()")
public Object around(ProceedingJoinPoint joinPoint) {
// 保证目标方法被执行
Object[] args = joinPoint.getArgs();
Object result = null;
try {
// 增强代码 -> Before
System.out.println("开启事务");
// 触发目标方法的执行
result = joinPoint.proceed(args);
System.out.println("结束事务");
} catch (Throwable e) {
System.out.println("事务回滚");
throw new RuntimeException(e);
} finally {
}
return result;
}
}
简化了前面 4种(@Before、@AfterReturning、@AfterThrowing、@After)
