Spring学习(十)-AOP返回通知&异常通知&环形通知
来源:互联网 发布:柠檬网络电视tv免费版 编辑:程序博客网 时间:2024/06/06 01:56
返回通知 异常通知 环形通知
1.LoggingAspect.java切面类
环形通知包括所有前置 后置 返回 异常
package com.spring.aop.impl2;import java.util.Arrays;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.springframework.stereotype.Component;@Aspect@Componentpublic class LoggingAspect {// /*// * 在方法正常结束受执行的代码// * 返回通知可以访问到返回值// */// @AfterReturning(value="execution(public int com.spring.aop.impl2.AtithmeticCalculatior.*(..))",// returning="result")// public void afterReturning(JoinPoint joinPoint,Object result)// {// String methodname=joinPoint.getSignature().getName();// System.out.println("The method "+methodname+" ends with "+result);// }// // /*// * 在目标方法出现异常时执行的代码// * 可以访问到异常对象,且可以指定在出现特定异常时在执行通知代码// */// @AfterThrowing(value="execution(public int com.spring.aop.impl2.AtithmeticCalculatior.*(..))",// throwing="ex")// public void afterThrowing(JoinPoint joinPoint,Exception ex)// {// String methodname=joinPoint.getSignature().getName();// System.out.println("The method "+methodname+" occurs excetion: "+ex);// } /* * 环绕通知需要携带ProceedingJoinPoint类型的参数 * 环绕通知类似于动态代理的全过程ProceedingJoinPoint类型的参数可以决定是否执行目标方法 * 且环绕通知必须有返回值,返回值即目标方法的返回值 */ @Around("execution(public int com.spring.aop.impl2.AtithmeticCalculatior.*(..))") public Object aroundMethod(ProceedingJoinPoint pjd) { String methodname=pjd.getSignature().getName(); Object result=null; try { //前置通知 System.out.println("The method begins "+methodname+" begins with "+Arrays.asList(pjd.getArgs())); //执行目标方法 result=pjd.proceed(); //返回通知 System.out.println("The method "+methodname+" ends with "+result); } catch (Throwable e) { //异常通知 System.out.println("The method "+methodname+" occurs exception: "+e); e.printStackTrace(); } //后置通知 System.out.println("The method "+methodname+" ends"); return result; }}
2.切面AOP的优先级
依靠@Order(num) num 越小优先级越高
另一个切面验证类
package com.spring.aop.impl2;import java.util.Arrays;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Order(1)@Aspect@Componentpublic class VlidationAspect { @Before("execution(public int com.spring.aop.impl2.AtithmeticCalculatior.*(..))") public void validateArgs(JoinPoint joinPoint) { System.out.println("validate: "+Arrays.asList(joinPoint.getArgs())); }}
3.重用切点表达式
package com.spring.aop.impl2;import java.util.Arrays;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Order(2)@Aspect@Componentpublic class LoggingAspect { /* * 定义一个方法 用于声明切入点表达式,该方法中不需要添加其他代码 * 只需要把方法名复制给各个方法 * */ @Pointcut("execution(public int com.spring.aop.impl2.AtithmeticCalculatior.*(..))") public void declareJointPointExpression() { } /* * 在方法正常结束受执行的代码 * 返回通知可以访问到返回值 */ @AfterReturning(value="declareJointPointExpression()") public void afterReturning(JoinPoint joinPoint,Object result) { String methodname=joinPoint.getSignature().getName(); System.out.println("The method "+methodname+" ends with "+result); } /* * 在目标方法出现异常时执行的代码 * 可以访问到异常对象,且可以指定在出现特定异常时在执行通知代码 */ @AfterThrowing(value="declareJointPointExpression()") public void afterThrowing(JoinPoint joinPoint,Exception ex) { String methodname=joinPoint.getSignature().getName(); System.out.println("The method "+methodname+" occurs excetion: "+ex); } return result; }}
外部的类中实现重用表达式
package com.spring.aop.impl2;import java.util.Arrays;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Order(1)@Aspect@Componentpublic class VlidationAspect { /* 在同一包下时只需写类名方法就行 ,若不是同一包下需要写上包名 com.spring,aop.impl2 */ @Before("LoggingAspect.declareJointPointExpression()") public void validateArgs(JoinPoint joinPoint) { System.out.println("validate: "+Arrays.asList(joinPoint.getArgs())); }}
阅读全文
0 0
- Spring学习(十)-AOP返回通知&异常通知&环形通知
- Spring AOP学习笔记(3):AOP返回通知&异常通知&环绕通知
- Spring AOP--返回通知,异常通知和环绕通知
- Spring AOP--返回通知,异常通知和环绕通知(顶)
- Spring aop 前置通知、后置通知、返回通知、 异常通知 、后置通知
- Spring-AOP-通知-异常通知
- Spring笔记4-AOP,前置通知,后置通知,返回通知,异常通知
- spring学习笔记(17)——返回通知&异常通知&环绕通知
- spring(十):aop环绕通知
- Spring返回通知、异常通知和环绕通知
- Spring 基于注解,实现 前置通知、后置通知、返回通知、 异常通知 、后置通知
- 17.Spring学习笔记_返回通知&异常通知&环绕通知(by尚硅谷_佟刚)
- Spring AOP-通知-前置通知
- Spring AOP-通知-后置通知
- Spring-AOP-通知-环绕通知
- Spring学习(九)-AOP切面通知
- 13.返回通知&异常通知&环绕通知
- Spring4笔记----AOP的返回通知和异常通知
- SpringMVC配置实例
- MySQL常见面试题
- 【Python3.6爬虫学习记录】(十)爬取教务处成绩并保存到Excel文件中(哈工大)
- 机器学习MATLAB函数
- hdu[6147] Pokémon GO II
- Spring学习(十)-AOP返回通知&异常通知&环形通知
- hdu 6138
- POJ 1019 Number Sequence
- C语言基础知识
- [BZOJ] 3674 可持久化并查集加强版(主席树广泛运用)
- 【zzuli 2181】(GJJ的日常之暴富梦)
- python学习八(私有属性、__del__(self)方法)
- CodeForces
- EXCEL ADDIN 学习(一)