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





原创粉丝点击