spring学习笔记十五 切面的优先级

这一节是对上一节的补充,在上一节的基础上,我们再写一个切面,里面也有一个前置通知;那么问题来了:先执行哪个前置通知呢?

所以必须要有一个认为的给它定义一个顺序。

VlidationAspect.java

package aop.impl;

import java.util.Arrays;

import org.aopalliance.intercept.Joinpoint;
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;

@Aspect
@Component
public class VlidationAspect {
    @Before("execution(public int aop.impl.AtithmeticCalculator.*(..))")    
	public void validateArgs(JoinPoint joinpoint){
		System.out.println("-->validata"+Arrays.asList(joinpoint.getArgs()));
	}
}

LoggingAspect.java

package aop.impl;

import java.util.Arrays;
import java.util.List;

import javax.management.RuntimeErrorException;

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

import com.mysql.fabric.xmlrpc.base.Array;

/*
 * 在aop.impl.AtithmeticCalculator接口的每一个实现类的每一个方法之前执行
 * 
 * */
@Aspect
@Component
public class LoggingAspect {

	@Before("execution(public int aop.impl.AtithmeticCalculator.*(..))")
	public void beforeMethod(JoinPoint joinPoint){
		String methodName=joinPoint.getSignature().getName();
		Object[] args=joinPoint.getArgs();
		System.out.println("The Method "+methodName+"begins"+Arrays.asList(args));
		
	}
	@After("execution(public int aop.impl.AtithmeticCalculator.*(..))")
	public void afterMethod(JoinPoint joinPoint){
		
		String methodName=joinPoint.getSignature().getName();
		System.out.println("The Method "+methodName+" end");
		
	}
	
	 /* 在方法正常结束后执行的代码
	 * 返回通知是可以访问到方法的返回值
	 */ 
	@AfterReturning(value="execution(public int aop.impl.AtithmeticCalculator.*(..))",
			returning="result")
	public void afterReturning(JoinPoint joinPoint,Object result){
		
		String methodName=joinPoint.getSignature().getName();
		System.out.println("The Method "+methodName+" end with"+result);
		
	}
	
	 /* 异常通知
	 * 在目标方法出现异常时会执行的代码
	 * 可以访问到异常对象,且可以指定在出现特定异常时再执行通知代码
	 */ 
	@AfterThrowing(value="execution(public int aop.impl.AtithmeticCalculator.*(..))",
			throwing="ex")
	public Object afterThrowing(JoinPoint joinPoint,Exception ex){
		String methodName=joinPoint.getSignature().getName();
		System.out.println("The Method "+methodName+" occurs excetion"+ex);
		return 100;
	}
	
	/* 环绕通知需要携带ProceedingJoinPoint类型的参数
	 * 环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行目标方法
	 * 且环绕通知必须有返回值,返回值即为目标方法的返回值
	 * 环绕通知是最强的,但是不是最常用的
	 * */
	/*@Around("execution(public int aop.impl.AtithmeticCalculator.*(..))")
	public Object aroundMethod(ProceedingJoinPoint pjd){
		
		Object result=null;
		String methodName=pjd.getSignature().getName();
		try {
			//前置通知
			System.out.println("The method"+methodName+" begins "+Arrays.asList(pjd.getArgs()));
			//执行目标方法
			result=pjd.proceed();
			//返回通知
			System.out.println("The "+methodName+"  ends result:"+result);
		} catch (Throwable e) {
			// 异常通知
			System.out.println("The "+methodName+" occurs exception:"+e);
			throw new RuntimeException(e);
		}
		//后置通知
		System.out.println("The method"+methodName+"ends");
		return result;
	}*/
}

运行结果:

com.sun.proxy.$Proxy10
The Method addbegins[1, 5]
-->validata[1, 5]
The Method add end
The Method add end with6
6
The Method divbegins[1, 1]
-->validata[1, 1]
The Method div end
The Method div end with1
1


如果设置任何东西,那么先执行的是我们之前写的那个前置通知,但是我们将定义一个顺序之后,就不一样了。

在LoggingAspect这个类的@Aspect 注解前面添加一个,@Order(2);

同样在VlidationAspect类的@Aspect 注解前面添加一个,@Order(1);

@Order里面的值越小,执行的顺序越靠前。所以执行结果是:先执行VlidationAspect前置通知,执行结果如下:


com.sun.proxy.$Proxy11
-->validata[1, 5]
The Method addbegins[1, 5]
The Method add end
The Method add end with6
6
-->validata[1, 1]
The Method divbegins[1, 1]
The Method div end
The Method div end with1
1





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不染心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值