SpringBoot AOP Usage Notes

  • 2021-06-28 12:22:59
  • OfStack

1. Enable AOP

a.Add the @Aspect annotation to the class

b.Inject this class, using @Component to inject into the Spring container

2. Create entry points from PointCut objects

a. Inject in a method similar to the following


@Pointcut("execution(* com.sguess.service.IAOPService.*(..))")
  private void pointcut() {
  }

i. where the execution expression is
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
ii. Note that the pointcut () method name is required for subsequent entry
iii. Method can write nothing, write nothing, can not be transferred
iv. You can also create multiple PointCuts, such as one more


@Pointcut("execution(* com.sguess.service.IAOPService.fun1(..))")
    private void pointcut2() {
    }

This method name is in place pointcut2, the method name is not the same.

b.Create After method, Before method


@After(value = "pointcut()")
  public void doAfter() {
    System.out.println("Do AOP After function 01");
  }

The i. After method is when a configured entry point is executed.
pointcut () in ii. value is the method name we created @Pointcut earlier. In other words, it is matched by the method name and the entry point.
iii. This method name can be arbitrary.
The iv. Before method is the same

c.After method with Return,


@AfterReturning(returning = "str", pointcut = "pointcut()")
  public void doAfterReturning(String str) throws Exception {
    System.out.println("Return value is: " + str);
  }

i. AfterReturn refers to getting the return value of the cut-in method after it has been executed, then executing the method. Note the key that this can operate on the return value.
ii. returning = "str" means that the value variable returned by the cut-in method is assumed to be str
The parameter variable names of the doAfterReturning (String str) method must match those of returning, which is also called str.
iii. pointcut = "pointcut()" also refers to the previously declared pointcut method name

3. Use entry points through annotations

a. Listening Method Parameters


@Before("execution(public int com.sguess.service.*(int, int))")
  public void beforMethod(JoinPoint point) {
    String methodName = point.getSignature().getName();
    List<Object> args = Arrays.asList(point.getArgs());
    System.out.println("Before FunctionName : " + methodName + ",ParameterName : " + args);
  }
  @After("execution(public int com.sguess.service.*(int, int))")
  public void afterMethod(JoinPoint point) {
    String methodName = point.getSignature().getName();
    List<Object> args = Arrays.asList(point.getArgs());
    System.out.println("After FunctionName : " + methodName + ",ParameterName : " + args);
  }

4. Execution order:

The a.Around method takes precedence over Before/After and After takes precedence over AfterReturn.

i. Code


@Before("execution(public int com.sguess.service.*.*(int, int))")
      public void beforMethod(JoinPoint point) {
        System.out.println("Before function");
      }
      @After("execution(public int com.sguess.service.*.*(int, int))")
      public void afterMethod(JoinPoint point) {
        System.out.println("After function");
      }
      @AfterReturning("execution(public int com.sguess.service.*.*(int, int))")
      public void afterReturnMethod(JoinPoint point) {
        System.out.println("AfterReturn function");
      }
      @AfterThrowing(value = "execution(public int com.sguess.service.*.*(int, int))", throwing = "e")
      public void afterReturningThrowing(JoinPoint point, Exception e) {
        System.out.println("AfterReturnThrowing function");
      }
      @Around("execution(public int com.sguess.service.*.*(int, int))")
      public Object aroundMethod(ProceedingJoinPoint pdj) {
        System.out.println("Start AroundFunction");
        Object result = null;
        try {
          System.out.println("Around process start");
          result = pdj.proceed();
          System.out.println("Around process end");
        } catch (Throwable e) {
          System.out.println("Around process exception");
        }
        System.out.println("After Around process");
        return result;
      }
    }

Execution results:

Start AroundFunction
Around process start
Before function
Around process end
After Around process
After function
AfterReturn function

5. Summary:


  @AfterReturning(returning = "str", pointcut = "pointcut()")
  public void doAfterReturning(String str) throws Exception {
    System.out.println("Return value is: " + str);
  }
  @Before("execution(public int com.sguess.service.*.*(int, int))")
  public void beforMethod(JoinPoint point) {
    String methodName = point.getSignature().getName();
    List<Object> args = Arrays.asList(point.getArgs());
    System.out.println("Before FunctionName : " + methodName + ",ParameterName : " + args);
  }
  @After("execution(public int com.sguess.service.*.*(int, int))")
  public void afterMethod(JoinPoint point) {
    String methodName = point.getSignature().getName();
    List<Object> args = Arrays.asList(point.getArgs());
    System.out.println("After FunctionName : " + methodName + ",ParameterName : " + args);
  }
  @AfterThrowing(value = "execution(public int com.sguess.service.*.*(int, int))", throwing = "e")
  public void afterReturningThrowing(JoinPoint point, Exception e) {
    String methodName = point.getSignature().getName();
    List<Object> args = Arrays.asList(point.getArgs());
    System.out.println("AfterReturningThrowing FunctionName : " + methodName + ",ParameterName : " + args + ",Exception : " + e);
  }
  @Around("execution(public int com.sguess.service.*.*(int, int))")
  public Object aroundMethod(ProceedingJoinPoint pdj) {
      System.out.println("Start AroundFunction");
      Object result = null;
      try {
          System.out.println("Around process start");
          result = pdj.proceed();
          System.out.println("Around process end");
      } catch (Throwable e) {
          System.out.println("Around process exception");
      }
      System.out.println("After Around process");
      return result;
  }

summary


Related articles: