spring--(19)切面優先級

假設工程中有兩個切面:驗證切面和日誌切面,咱們須要驗證切面在日誌切面的前面,則須要作以下配置
驗證切面以下java

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

日誌切面以下spring

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;
	}
}
相關文章
相關標籤/搜索