Spring学习 之 AOP切面执行顺序的控制

@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));
	}

}

输出结果就是

先执行数据检证,后执行日志出力。

发布了21 篇原创文章 · 获赞 0 · 访问量 3434
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章