Spring的AOP五大通知注解
发布日期:2021-05-10 02:13:31 浏览次数:15 分类:精选文章

本文共 4329 字,大约阅读时间需要 14 分钟。

������������������������������


Spring AOPNotify Annotation Explanation and Practice

Spring AOP���Spring���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

Modify the add Method

������������������������������������add���������

@Override
public int add(int a, int b) {
System.out.println("������add������");
return a + b;
}

@Before Annotation: Executed Before the Method

@Before������������������������������������������������������������������������������������������������������������������������

@Aspect
@Component
public class MyAspect {
@Before("execution(* calculator.CalculatorImpl.*(..))")
public void beforeMethod(JoinPoint joinpoint) {
String methodName = joinpoint.getSignature().getName();
List list = Arrays.asList(joinpoint.getArgs());
System.out.println("���������" + methodName + " ������������" + list);
}
}

Output:

���������add ������������[1, 2] ������add������

@After Annotation: Executed After Method Completion

@After������������������������������������������������������������������������������������������������������������������������@Before������������������������

@After("execution(* calculator.CalculatorImpl.*(..))")
public void afterMethod(JoinPoint joinpoint) {
String methodName = joinpoint.getSignature().getName();
List list = Arrays.asList(joinpoint.getArgs());
System.out.println("���������" + methodName + " ������������" + list);
}

Output:

������add������ ���������add ������������[1, 2]

@AfterReturning Annotation: Executed After Method Completion and Can Access Return Value

@AfterReturning������������������������������������������������������������������������������������������������������������������������������������������������������������������

@AfterReturning(value = "execution(* calculator.CalculatorImpl.*(..))", returning = "result")
public void afterMethod(JoinPoint joinpoint, Object result) {
String methodName = joinpoint.getSignature().getName();
System.out.println("���������" + methodName + " ������������" + result);
}

Output:

������add������ ���������add ������������33

@AfterThrowing Annotation: Executed After Exception Occurrence

@AfterThrowing���������������������������������������������������������������������������������������������������������������������������������������������������������������������

@AfterThrowing(value = "execution(* calculator.CalculatorImpl.*(..))", throwing = "ex")
public void afterMethod(JoinPoint joinpoint, Exception ex) {
String methodName = joinpoint.getSignature().getName();
System.out.println("���������" + methodName + "���������������" + ex);
}

@Around Annotation: Wrap Around the Method Execution

@Around������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

@Around("execution(public * calculator.CalculatorImpl.*(..))")
public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) {
Object result = null;
String methodName = proceedingJoinPoint.getSignature().getName();
try {
System.out.println("���������������������������������������" + methodName + " ���������" + proceedingJoinPoint.getArgs());
result = proceedingJoinPoint.proceed();
System.out.println("���������������������" + result);
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("������������");
}
System.out.println("���������������" + methodName);
result = 100;
return result;
}

Output:

���������������������������������������add[1, 2] ������add������ ���������������������3 ���������������add

Key Notes

���@Around������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������


���������������Spring AOP������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:AOP切面的优先级Order属性
下一篇:Spring+Maven+Aspectj的AOP实现对指定对象的切面拦截

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月21日 02时35分18秒