Spring学习笔记十四--Aspect切面优先级
2016-03-10 00:00
579 查看
package aopa; 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 @Component public class ValidAspect { @Before("execution(public int aopa.CalImpl.*(..))") public void beforMethod(JoinPoint joinPoint) { System.out.println("ValidAspect beforeMethod()...."); } }
package aopa; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.util.Arrays; import java.util.Objects; @Order(2) @Aspect @Component public class Logging { /* //前置通知 @Before("execution(public int aopa.CalImpl.*(..))") public void beforeMethod(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); Object[] args = joinPoint.getArgs(); System.out.println("前置通知 " + "MethodName : " + methodName + " Args : " + Arrays.asList(args)); } //后置通知,无论该方法是否出现异常 @After("execution(public int aopa.CalImpl.*(..))") public void afterMethod(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); Object[] args = joinPoint.getArgs(); System.out.println("后置通知 " + "MethodName : " + methodName + " Args : " + Arrays.asList(args)); } //返回通知,可以访问方法返回值 @AfterReturning(value = "execution(public int aopa.CalImpl.*(..))", returning = "result") public void afterReturn(JoinPoint joinPoint, Object result) { String methodName = joinPoint.getSignature().getName(); Object[] args = joinPoint.getArgs(); System.out.println("后置返回通知 " + "MethodName : " + methodName + " Args : " + Arrays.asList(args) + " result : " + result); } //出现异常时候执行的代码 @AfterThrowing(value = "execution(public int aopa.CalImpl.*(..))", throwing = "ex") public void afterThrow(JoinPoint joinPoint, Exception ex) { String methodName = joinPoint.getSignature().getName(); Object[] args = joinPoint.getArgs(); System.out.println("异常 3ff0 通知 " + "MethodName : " + methodName + " Exception : " + ex); } */ //环绕通知ProceedingJoinPoint参数,是否执行目标方法,必须有目标方法返回值,相当于动态代理 @Around("execution(public int aopa.CalImpl.*(..))") public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) { Object result = null; String methodName = proceedingJoinPoint.getSignature().getName(); Object[] args = proceedingJoinPoint.getArgs(); System.out.println("AroundMethod ...."); //执行目标方法 try { //前置通知 System.out.println("前置通知 " + "MethodName : " + methodName + " Args : " + Arrays.asList(args)); result = proceedingJoinPoint.proceed(); System.out.println("后置返回通知 " + result); } catch (Throwable throwable) { throwable.printStackTrace(); System.out.println("异常通知 " + "MethodName : " + methodName + " Exception : " + throwable); throw new RuntimeException(); } System.out.println("后置通知 "); return result; } }
相关文章推荐
- Spring学习笔记十四--AspectJ重用切点表达式
- 七天学会ASP.NET MVC(1-3)源代码下载
- asp.net 防止SQL注入攻击
- ASP Girdview通过ButtonField获取行号与主键
- 用asp.net c# HttpWebRequest获取网页源代码
- ASP.NET MVC5 视图预编译
- Asp.Net MVC EF各版本区别
- casperjs在windows下抓取元素乱码
- 树莓派折腾录一. 必要的初始化(Raspberry Pi 2 model B + raspbian jessie)
- ASP.NET验证控件详解
- ASP.NET MVC ajax数组,模型绑定问题。
- Asp.Net异步编程
- Asp.net 上传文件小叙(修改FileUpload显示文字等)
- asp.net Web.config中assembly注册程序集的目的
- 【OpenStack】OpenStack系列11之namaspace&openvswitch原理实践
- 基于ASP.NET MVC(C#)和Quartz.Net组件实现的定时执行任务调度
- Asp.Net 上传图片并生成高清晰缩略图(转)
- asp.net中session的原理及应用
- ASP.NET Web API中如何传递参数
- Asp.Net MVC使用ViewData导致双引号被转义的解决方法