版本信息

  • SpringBoot: 2.3.2.RELEASE
  • Spring-Core: 5.2.8.RELEASE

AOP切面代码

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author hubz
 * @date 2022/1/8 22:08
 **/
@Aspect
@Component
public class TestAspect {

    @Pointcut("execution(* com.springboot.service.TestAopImpl.*(..))")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint point) {
        try {
            System.out.println("===============进入Around===============");
            Object res = point.proceed();
            System.out.println("===============离开Around===============");
            return res;
        } catch (Throwable throwable) {
            System.out.println("===============Around抛出报错信息===============");
            throwable.printStackTrace();
        }
        return null;
    }

    @Before("pointcut()")
    public void before() {
        System.out.println("===============@Before===============");
    }

    @After("pointcut()")
    public void after() {
        System.out.println("===============@After===============");
    }

    @AfterReturning(returning = "result", pointcut = "pointcut()")
    public void afterReturning(Object result) {
        System.out.println("===============@AfterReturning===============");
        // 返回结果
        System.out.println("@AfterReturning-->返回值:" + result);
    }

    @AfterThrowing(value = "pointcut()", throwing = "a")
    public void afterThrowing(Throwable a) {
        System.out.println("===============@AfterThrowing===============");
        System.out.println("@AfterThrowing-->未处理的报错信息:" + a.getMessage());
    }
}

执行方法代码

import org.springframework.stereotype.Service;

/**
 * @author hubz
 * @date 2022/1/8 22:13
 **/
@Service
public class TestAopImpl {

    public String testAOPOrder(String abc) {
        System.out.println("===============【testAOPOrder方法】==============="+abc);
        return "hello world";
    }
}

执行

正常执行的结果

===============进入Around===============
===============@Before===============
===============【testAOPOrder方法】===============haha
===============@AfterReturning===============
@AfterReturning-->返回值:hello world
===============@After===============
===============离开Around===============

当方法中存在报错时的结果

===============进入Around===============
===============@Before===============
===============@AfterThrowing===============
@AfterThrowing-->未处理的报错信息:/ by zero
===============@After===============
===============Around抛出报错信息===============
java.lang.ArithmeticException: / by zero
	at com.springboot.service.TestAopImpl.testAOPOrder(TestAopImpl.java:13)
	at com.springboot.service.TestAopImpl$$FastClassBySpringCGLIB$$f9a7f095.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
	at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
	at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:55)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
	at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:47)
...