spring--(19)切面优先级

本文介绍了如何在Spring AOP中通过@Order注解配置验证切面和日志切面的执行顺序,确保验证切面在日志切面之前执行。

假设工程中有两个切面:验证切面和日志切面,我们需要验证切面在日志切面的前面,则需要做如下配置
验证切面如下

package com.test.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(1)
@Aspect
@Component
public class Validation {
	
	@Before("execution(* com.test.aop.impl.CalculatorImpl.*(int,int))")
	public void validateArgs(JoinPoint joinPoint) {
		System.out.println("validate:"+Arrays.asList(joinPoint.getArgs()));
	}
}

日志切面如下

package com.test.aop.impl;

import java.util.Arrays;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 切面类
 * 1.需要将该类放入到IOC容器中
 * 2.再将它申明为一个切面
 */

@Order(2)
@Aspect
@Component
public class LoggingAspect {
	
	//环绕通知
	@Around("execution(* com.test.aop.impl.CalculatorImpl.*(int,int))")
	public Object Around(ProceedingJoinPoint proceedingJoinPoint){
		
		Object result = null;
		String methodName = proceedingJoinPoint.getSignature().getName();
		try {
			//前置通知
			System.out.println("The method"+methodName+"begin with:"+Arrays.asList(proceedingJoinPoint.getArgs()));
			result = proceedingJoinPoint.proceed();
			//返回通知
			System.out.println("The method" + methodName + "end with:"+result);
		} catch (Throwable e) {
			//异常通知
			System.out.println("The method" + methodName + "occurs exception:"+e);	
		}
		//后置通知
		System.out.println("The method" + methodName + "end ");
		return result;
	}
}

转载于:https://my.oschina.net/u/2312022/blog/741244

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值