1.可以使用@Order注解指定切面的优先级,值越小优先级越高
示例:
VlidationAspect.java:
1 package com.hk.spring.aop.notice; 2 3 import java.util.Arrays; 4 5 import org.aopalliance.intercept.Joinpoint; 6 import org.aspectj.lang.JoinPoint; 7 import org.aspectj.lang.annotation.Aspect; 8 import org.aspectj.lang.annotation.Before; 9 import org.springframework.core.annotation.Order; 10 import org.springframework.stereotype.Component; 11 12 @Order(1) 13 @Aspect 14 @Component 15 public class VlidationAspect { 16 17 @Before("execution(public int com.hk.spring.aop.notice.ArithmeticCalculator.*(..))") 18 public void validateArgs(JoinPoint joinPoint){ 19 System.out.println("-->validate:" + Arrays.asList(joinPoint.getArgs())); 20 } 21 22 }
1 package com.hk.spring.aop.notice; 2 3 import java.util.Arrays; 4 5 import org.aopalliance.intercept.Joinpoint; 6 import org.aspectj.lang.JoinPoint; 7 import org.aspectj.lang.ProceedingJoinPoint; 8 import org.aspectj.lang.annotation.AfterReturning; 9 import org.aspectj.lang.annotation.AfterThrowing; 10 import org.aspectj.lang.annotation.Around; 11 import org.aspectj.lang.annotation.Aspect; 12 import org.aspectj.lang.annotation.Before; 13 import org.springframework.core.annotation.Order; 14 import org.springframework.stereotype.Component; 15 16 @Order(2) 17 @Aspect 18 @Component 19 public class LoggingAspect { 20 /* 21 * 在方法正常执行后执行的通知叫返回通知 22 * 返回通知是可以访问到方法的返回值的 23 */ 24 // @AfterReturning(value="execution(public int com.hk.spring.aop.notice.ArithmeticCalculator.*(..))", 25 // returning="result") 26 // public void afterReturning(JoinPoint joinPoint,Object result){ 27 // String methodName = joinPoint.getSignature().getName(); 28 // System.out.println("The method " + methodName + " ends with " + result); 29 // } 30 31 /* 32 * 在目标方法出现异常时,会执行代码。 33 * 可以访问到异常对象;且可以指定在出现特定异常时在执行通知 34 */ 35 // @AfterThrowing(value="execution(public int com.hk.spring.aop.notice.ArithmeticCalculator.*(..))", 36 // throwing="ex") 37 // public void afterThrowing(JoinPoint joinPoint,Exception ex){ 38 // String methodName = joinPoint.getSignature().getName(); 39 // System.out.println("The method " + methodName + " coours exception : " + ex); 40 // } 41 42 /* 43 * 环绕通知需要携带ProceedingJoinPoint 类型的参数 44 * 环绕通知类似于动态代理的全过程:ProceedingJoinPoint这个类型的参数可以决定是否执行目标方法 45 * 且环绕通知必须有返回值,返回值即为目标方法的返回值 46 */ 47 @Around("execution(public int com.hk.spring.aop.notice.ArithmeticCalculator.*(..))") 48 public Object aroundMethod(ProceedingJoinPoint pjd){ 49 50 Object result = null; 51 String methodName = pjd.getSignature().getName(); 52 53 //执行目标方法 54 try { 55 //前置通知 56 System.out.println("The method " + methodName + "begins with " + Arrays.asList(pjd.getArgs())); 57 result = pjd.proceed(); 58 //后置通知 59 System.out.println("The method " + methodName + "ends with " + result); 60 } catch (Throwable e) { 61 //异常通知 62 System.out.println("The method occurs exception:" + e); 63 } 64 //后置通知 65 System.out.println("The method " + methodName + " ends"); 66 return result; 67 } 68 }
Main.java:
1 package com.hk.spring.aop.notice; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 public class Main { 7 8 public static void main(String[] args) { 9 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 10 ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("ArithmeticCalculator"); 11 12 System.out.println(arithmeticCalculator.getClass().getName()); 13 14 int result = arithmeticCalculator.add(1, 2); 15 System.out.println("result: " + result); 16 17 result = arithmeticCalculator.div(1000, 10); 18 System.out.println("result " + result); 19 20 } 21 22 }
运行结果:
由结果可以看出,验证切面优先于日志切面。