
本文共 4329 字,大约阅读时间需要 14 分钟。
������������������������������
Spring AOPNotify Annotation Explanation and Practice
Spring AOP���Spring���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
Modify the add Method
������������������������������������add���������
@Overridepublic int add(int a, int b) { System.out.println("������add������"); return a + b;}
@Before Annotation: Executed Before the Method
@Before������������������������������������������������������������������������������������������������������������������������
@Aspect@Componentpublic class MyAspect { @Before("execution(* calculator.CalculatorImpl.*(..))") public void beforeMethod(JoinPoint joinpoint) { String methodName = joinpoint.getSignature().getName(); 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
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������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
