[Spring Boot] 032 - configure AOP

[Spring Boot] 032 - configure AOP

1, AOP

1. Overview

In the following reference articles, AOP has been introduced in detail, but I am very unfamiliar because I don't often use (never used) AOP, so I take this opportunity to learn about AOP again,

2. Reference articles

[Spring]009-AOP

https://blog.csdn.net/qq_29689343/article/details/108410074

2, Configuring AOP with Spring Boot

1. Code demonstration

Step 1: introduce dependency

<!--aop-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

Step 2: prepare a HelloService

package com.zibo.api.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class HelloService {
    public String getName(){
        log.warn("HelloService ==> getName");
        return "zibo";
    }

    public String getInfo(){
        log.warn("HelloService   ==> getInfo");
        return "no info";
    }
}

Step 3: write aspect LogAspect

package com.zibo.api.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class LogAspect {
    @Pointcut("execution(* com.zibo.api.service.*.*(..))")
    public void pc1(){
    }
    
    @Before("pc1()")
    public void before(JoinPoint jp){
        String name = jp.getSignature().getName();
        log.warn(name + "Method start execution");
    }
    
    @After("pc1()")
    public void after(JoinPoint jp){
        String name = jp.getSignature().getName();
        log.warn(name + "End of method execution");
    }
    
    @AfterReturning(value = "pc1()", returning = "result")
    public void afterReturning(JoinPoint jp, Object result){
        String name = jp.getSignature().getName();
        log.warn(name + "The return value of the method is" + result);
    }
    
    @AfterThrowing(value = "pc1()", throwing = "e")
    public void afterThrowing(JoinPoint jp, Exception e){
        String name = jp.getSignature().getName();
        log.warn(name + "Method throws an exception. The exception is" + e.getMessage());
    }
    
    @Around("pc1()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        return pjp.proceed();
    }
}

2. Operation results

(omitted)

3. Annotation description

@Aspect annotation: indicates that this is an aspect class;

@Pointcut annotation: indicates that this is a pointcut. In execution, the first * represents any return value, the second * represents any class under the package, the third * represents any method in the class, and the two... In parentheses represent any parameters;

@Before annotation: indicates that this is a pre notification, which is executed before the target method is executed. The method name, modifier and other information of the target method can be obtained through the JoinPoint parameter;

@After annotation: indicates that this is a post notification, which is executed after the target method is executed;

@AfterReturning annotation: indicates that this is a return notification. The return value of the target method can be obtained in this method. The return parameter of @ AfterReturning annotation refers to the variable name of the return value and the parameter of the corresponding method. Note that the type of result defined in the method parameter is Object, indicating that the return value of the target method can be of any type. If the type of result is Long, the method can only handle the case where the return value is Long.

@AfterThrowing annotation: indicates that this is an Exception notification, that is, when an Exception occurs in the target method, the method will be called. The Exception type is Exception, which means that all exceptions will enter the method for execution. If the Exception type is arithmetexception, it means that only the arithmetexception Exception thrown by the target method will enter the method for execution.

@Around annotation: indicates that this is a surround notification, and the surround notification is the most powerful notification among all notifications! It can realize the functions of pre notification, post notification, exception notification and return notification. After the target method enters the surround notification, the target method can continue to execute by calling the processed method of the proceed ingjoinpoint object. The developer can modify the execution parameters and return values of the target method, and handle the exceptions of the target method here.

Keywords: Java Spring Spring Boot

Added by DarkHavn on Wed, 22 Dec 2021 05:46:04 +0200