spring学习笔记十五 切面的优先级
来源:互联网 发布:如何给给淘宝号升级 编辑:程序博客网 时间:2024/05/21 09:15
这一节是对上一节的补充,在上一节的基础上,我们再写一个切面,里面也有一个前置通知;那么问题来了:先执行哪个前置通知呢?
所以必须要有一个认为的给它定义一个顺序。
VlidationAspect.java
package aop.impl;import java.util.Arrays;import org.aopalliance.intercept.Joinpoint;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;@Aspect@Componentpublic class VlidationAspect { @Before("execution(public int aop.impl.AtithmeticCalculator.*(..))") public void validateArgs(JoinPoint joinpoint){System.out.println("-->validata"+Arrays.asList(joinpoint.getArgs()));}}
LoggingAspect.java
package aop.impl;import java.util.Arrays;import java.util.List;import javax.management.RuntimeErrorException;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;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.Before;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;import com.mysql.fabric.xmlrpc.base.Array;/* * 在aop.impl.AtithmeticCalculator接口的每一个实现类的每一个方法之前执行 * * */@Aspect@Componentpublic class LoggingAspect {@Before("execution(public int aop.impl.AtithmeticCalculator.*(..))")public void beforeMethod(JoinPoint joinPoint){String methodName=joinPoint.getSignature().getName();Object[] args=joinPoint.getArgs();System.out.println("The Method "+methodName+"begins"+Arrays.asList(args));}@After("execution(public int aop.impl.AtithmeticCalculator.*(..))")public void afterMethod(JoinPoint joinPoint){String methodName=joinPoint.getSignature().getName();System.out.println("The Method "+methodName+" end");} /* 在方法正常结束后执行的代码 * 返回通知是可以访问到方法的返回值 */ @AfterReturning(value="execution(public int aop.impl.AtithmeticCalculator.*(..))",returning="result")public void afterReturning(JoinPoint joinPoint,Object result){String methodName=joinPoint.getSignature().getName();System.out.println("The Method "+methodName+" end with"+result);} /* 异常通知 * 在目标方法出现异常时会执行的代码 * 可以访问到异常对象,且可以指定在出现特定异常时再执行通知代码 */ @AfterThrowing(value="execution(public int aop.impl.AtithmeticCalculator.*(..))",throwing="ex")public Object afterThrowing(JoinPoint joinPoint,Exception ex){String methodName=joinPoint.getSignature().getName();System.out.println("The Method "+methodName+" occurs excetion"+ex);return 100;}/* 环绕通知需要携带ProceedingJoinPoint类型的参数 * 环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行目标方法 * 且环绕通知必须有返回值,返回值即为目标方法的返回值 * 环绕通知是最强的,但是不是最常用的 * *//*@Around("execution(public int aop.impl.AtithmeticCalculator.*(..))")public Object aroundMethod(ProceedingJoinPoint pjd){Object result=null;String methodName=pjd.getSignature().getName();try {//前置通知System.out.println("The method"+methodName+" begins "+Arrays.asList(pjd.getArgs()));//执行目标方法result=pjd.proceed();//返回通知System.out.println("The "+methodName+" ends result:"+result);} catch (Throwable e) {// 异常通知System.out.println("The "+methodName+" occurs exception:"+e);throw new RuntimeException(e);}//后置通知System.out.println("The method"+methodName+"ends");return result;}*/}
运行结果:
com.sun.proxy.$Proxy10
The Method addbegins[1, 5]
-->validata[1, 5]
The Method add end
The Method add end with6
6
The Method divbegins[1, 1]
-->validata[1, 1]
The Method div end
The Method div end with1
1
如果设置任何东西,那么先执行的是我们之前写的那个前置通知,但是我们将定义一个顺序之后,就不一样了。
在LoggingAspect这个类的@Aspect 注解前面添加一个,@Order(2);
同样在VlidationAspect类的@Aspect 注解前面添加一个,@Order(1);
@Order里面的值越小,执行的顺序越靠前。所以执行结果是:先执行VlidationAspect前置通知,执行结果如下:
com.sun.proxy.$Proxy11
-->validata[1, 5]
The Method addbegins[1, 5]
The Method add end
The Method add end with6
6
-->validata[1, 1]
The Method divbegins[1, 1]
The Method div end
The Method div end with1
1
- spring学习笔记十五 切面的优先级
- spring学习笔记(18)——切面的优先级
- spring学习笔记(20)----指定切面的优先级
- Spring切面的优先级
- 18.Spring学习笔记_切面的优先级(by尚硅谷_佟刚)
- Spring AOP学习笔记(4):AOP切面优先级&切面表达式重用&xml配置方式
- [Spring]Spring AOP学习笔记(2)---5种切入方式、AOP优先级及切面表达式的重用
- 切面优先级 Spring Aop
- 《Spring实战》学习笔记-第四章:面向切面的Spring
- 《Spring实战》学习笔记-第四章:面向切面的Spring
- Spring学习笔记-C4-面向切面的Spring
- 4.Spring学习笔记之面向切面的Spring
- Spring学习笔记三:面向切面的Spring
- 《Spring实战》学习笔记(三)面向切面的Spring
- Spring学习笔记(三) 面向切面的Spring
- Spring学习笔记(八) --- 面向切面的Spring
- 《Spring实战》学习笔记-第四章:面向切面的Spring
- Spring学习笔记3--面向切面(AOP)的例子
- 并行信号处理技术-序
- Graph Valid Tree
- HDU
- 多线程爬虫之糗事百科
- Git的简单使用
- spring学习笔记十五 切面的优先级
- Java并发编程:volatile关键字解析
- 9.11总结
- MySQL日期函数总结
- codility Max-Nonoverlapping-Segments
- HGDB(pg)行锁现象
- 102. Binary Tree Level Order Traversal
- node 异步编程Promise容器
- java中使用switch-case的用法及注意事项超全总结