@Order 註釋標籤來控制
當有複數個切面的時候 Order(n) // n的數值越小 ,執行的優先順就越靠前。
比如
package com.spring.apo;
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
@Component
public class VlidationAspect {
@Before("execution(* com.spring.apo.ArtthemticCalculator.*(..))")
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("【數據檢證】前置通知方法名: " + methodName + " 方法參數為:" + Arrays.asList(args));
}
}
package com.spring.apo;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(2)
@Aspect
@Component
public class LoggingAspect {
/**
* 在com.spring.apo.ArtthemticCalculator
* 的每一個方法執行之前 執行的處理
*/
@Before("execution(public int com.spring.apo.ArtthemticCalculator.*(..))")
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("【日誌出力】前置通知方法名: " + methodName + " 方法參數為:" + Arrays.asList(args));
}
}
輸出結果就是
先執行數據檢證,後執行日誌出力。