spring AOP 切面优先级 @order和重用切点表达式

如果有多个切面,有默认的先后执行顺序。但是可以用@Order(num)定义优先级,num越小,优先级越高。
新建一个ValidationAspect 切面,与原来的那个切面用@order方式确立先后运行顺序

package com.spring.aop.impl;

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注解指定切面的优先级,值越小优先级越高
*/
@Order(2)
@Aspect
@Component

public class ValidationAspect {
	@Before("LoggingAspect.declareJointPointExpression()")
       public void validateArgs(JoinPoint joinPoint) {
    	   System.out.println("validate:"+Arrays.asList(joinPoint.getArgs()));
       }
}

在先前的LoggingAspect(上一篇博客即可找到)里加上
切点表达式

/*定义一个方法,用于声明切入点表达式,一般,该方法中再不需要添入其他的代码
	 * 使用@Pointcut 来声明切入点表达式
	 * 后面的其他通知直接使用方法来引用当前的切入点表达式
	*/
	@Pointcut("execution(* com.spring.aop.impl.*.*(..))")
	  public void declareJointPointExpression() {}

@Before(“declareJointPointExpression()”)其他的类似,如果在不同的切面中,前面加上那个类就行,或者不在同一包中注意引用好包名
使用切入点,后面的切面就可以使用这个切点,写法更整洁。

猜你喜欢

转载自blog.csdn.net/qq_37774171/article/details/86530679