Day30 - specifies the priority of the facet

Posted by truckie2 on Sat, 01 Feb 2020 15:28:34 +0100

1, Knowledge reserve

  1. When more than one facet is applied to the same join point, their priority is uncertain unless explicitly specified
  2. The priority of facets can be specified by implementing the Ordered interface or by using the @ Order annotation
  3. Implement the Ordered interface. The smaller the return value of getOrder() method is, the higher the priority is
  4. If @ Order annotation is used, the sequence number appears in brackets. The smaller the value is, the higher the priority is

Two. Example

ValidationAspect.java

package com.atguigu.spring.aop.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;

/**
 * Verifying section
 * @author user
 *
 */
@Component
@Aspect
@Order(2) //Default int 2147483647 Max
public class ValidationAspect {
    
	@Before(value="execution(* com.atguigu.spring.aop.aspectJ.*.*(..))")
	public void beforeMethod(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();
		System.out.println("ValidationAspect==> The method " + methodName + " begin with : " + Arrays.asList(args));
		
	}
}

LoggingAspect.java

package com.atguigu.spring.aop.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.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Cut: log cut
 * @author user
 *
 */
@Component //Identify as a component
@Aspect //Identify as a tangent
@Order(3)
public class LoggingAspect {

	/**
	 * Pre notification: execute before method execution
	 * value: Pointcut expression
	 * Join point object: JoinPoint, which contains information about the current join point (i.e. the method currently used, i.e. the method in the pointcut expression)
	 * 
	 */
	@Before(value = "execution(public int com.atguigu.spring.aop.aspectJ.ArithmeticCalculatorImpl.add(int, int))")
	public void beforeMethod(JoinPoint joinPoint) {
		
		//Method name
		String methodName = joinPoint.getSignature().getName();
		
		//Parameter list for method
		Object[] args = joinPoint.getArgs();
		
		System.out.println("LoggingAspect==> The method " + methodName + " begin with : " + Arrays.asList(args));
		
	}
	
	
	/**
	 * Post notification: after the method is executed, it will execute regardless of whether the target method throws an exception or not
	 * Pointcut expression:
	 *     * : Any modifier any return value
	 *     * : Any class under package
	 *     * : Any method in class
	 *     ..: Any parameter list
	 */
	@After(value="execution(* com.atguigu.spring.aop.aspectJ.*.*(..))")
	public void afterMethod(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("LoggingAspect==> The method " + methodName + " ends.");
	}
	
	
	
	/**
	 * Exception notification: executed after the target method throws an exception
	 * Receiving exception information by throwing = "variable name", the name specified by throwing must be consistent with the parameter name used to receive the exception in the method
	 * 
	 * You can also specify what exception is thrown to perform exception notification by receiving the parameter type of the exception in the method
	 */
	@AfterThrowing(value="execution(* com.atguigu.spring.aop.aspectJ.*.*(..))", throwing="ex")
	public void afterThrowingMethod(JoinPoint joinPoint, Exception ex) {
		//Get method name
		String methodName = joinPoint.getSignature().getName();
		System.out.println("LoggingAspect==> The method " + methodName + " occurs Exception: " + ex);
	}
	
	
	/**
	 * Return notification: it is executed after the target method returns normally, and the return value of the method can be obtained
	 * Receive the return value of the method through returning = "variable name",
	 * returning The specified name must be the same as the parameter name used to receive the return value in the method
	 */
	@AfterReturning(value="execution(* com.atguigu.spring.aop.aspectJ.*.*(..))", returning="result")
	public void afterReturningMethod(JoinPoint joinPoint, Object result) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("LoggingAspect==> The method " + methodName + "ends with: " + result);
	}
	
	
	/**
	 * Circular notification: it is executed around the target method, which can be understood as a combination of pre post return exceptions, similar to the whole process of dynamic agent
	 * 
	 */
	@Around(value="execution(* com.atguigu.spring.aop.aspectJ.*.*(..))")
	public Object aroundMethod(ProceedingJoinPoint pjp) {
	
		try {
			//Preposition
			
			
			//Call target method
			Object result = pjp.proceed();
			
			
			//Return
			return result;
			
			
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			//abnormal
			e.printStackTrace();
		}finally {
			//Postposition
		}
	
		return null;
	
	}
	
	
}

196 original articles published, 16 praised, 10000 visitors+
Private letter follow

Topics: Spring Java