指定切面的优先级

指定切面的优先级

在同一个连接点上应用不止一个切面时, 除非明确指定, 否则它们的优先级是不确定的.

切面的优先级可以通过实现 Ordered 接口或利用 @Order 注解指定.

实现 Ordered 接口, getOrder() 方法的返回值越小, 优先级越高.

若使用 @Order 注解, 序号出现在注解中

package com.learn.spring.aspectJ;

import java.util.Arrays;

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.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 日志切面
 */
@Component  //标识为组件
@Aspect  //标识为切面
@Order(3)  //指定切面的优先级. 值越小,优先级越高. 标注@Order的切面比不标注@Order切面的优先级高
public class LoggingAspect {	
	
	/**
	 * 前置通知: 在方法执行之前执行.
	 * JoinPoint:连接点对象  包含了连接点相关的信息:方法名   方法的参数等....
	 */
	//@Before("execution(public int com.learn.spring.aspectJ.ArithmeticCalculator.add(int,int))")
	//@Before("execution(public int com.learn.spring.aspectJ.ArithmeticCalculator.*(int,int))")
	//任意修饰符任意返回值  com.learn.spring.aspectJ包下的任意类的任意方法   任意参数
	//@Before("execution(* com.learn.spring.aspectJ.*.*(..))")
	@Before("suibian()")
	public void beforeMethod(JoinPoint joinPoint){
		//获取方法名:
		String methodName = joinPoint.getSignature().getName();
		//获取参数
		Object [] args = joinPoint.getArgs();
		System.out.println("The method "+methodName+" begins with "+ Arrays.asList(args));
	}

}
package com.learn.spring.aspectJ;

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;

@Component
@Aspect
@Order(2)
public class ValidatorAspect {

	//@Before("execution(* com.learn.spring.aspectJ.*.*(..))")
	@Before("com.learn.spring.aspectJ.LoggingAspect.suibian()")
	public void beforeMethod(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		Object [] args = joinPoint.getArgs();
		System.out.println("Validator===>The method "+methodName+" begins with" + Arrays.asList(args));
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值