springboot aop 通知执行顺序(基于jdk1.8.0_341)

总结(around通知不写finally块代码)

无返回值&无异常: around1 - before - 函数执行 - after - around2

无返回值&有异常: around1 - before - 异常前函数执行 - afterThrowing - after - 控制台抛异常

有返回值&无异常: around1 - before - 函数执行 - afterReturning - after - around2 - 返回值

有返回值&有异常: around1 - before - 异常前函数执行 - afterThrowing - after - 控制台抛异常 - 返回null

------------------------------------代码-------------------------------------------------------------------

切面aspect

@Component
@Aspect
public class MyAspect {
    @Pointcut("execution(* com.example.aopdemo.service.MyService.hello(..))")
    public void pointcut(){}

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

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

    @Around("pointcut()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint){
        System.out.println("around-1");
        try {
            Object obj = joinPoint.proceed();
            System.out.println("around-2");
            return obj;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    @AfterReturning(value = "pointcut()",returning = "value")
    public void afterReturning(JoinPoint joinPoint, String value){
        System.out.println("afterReturning:"+value);
    }

    @AfterThrowing(value = "pointcut()",throwing = "e")
    public void afterThrowing(JoinPoint joinPoint,Exception e){
        System.out.println("afterThrowing:"+e);
    }
}

无返回值&无异常:

@Service
public class MyService {
    public void hello(){
        System.out.println("业务代码1");
        System.out.println("业务代码2");
    }
}
@SpringBootTest
class ProjectAopApplicationTests {

    @Autowired
    private MyService myService;

    @Test
    void contextLoads() {
        myService.hello();
    }

}

springboot aop 通知执行顺序(基于jdk1.8.0_341)_第1张图片

无返回值&有异常:

@Service
public class MyService {
    public void hello(){    
        System.out.println("业务代码1");
        int i=1/0;
        System.out.println("业务代码2");
    }
}
@SpringBootTest
class ProjectAopApplicationTests {

    @Autowired
    private MyService myService;

    @Test
    void contextLoads() {
        myService.hello();
    }
}

springboot aop 通知执行顺序(基于jdk1.8.0_341)_第2张图片

有返回值&无异常:

@Service
public class MyService {
    public String hello(){
        System.out.println("业务代码1");
        System.out.println("业务代码2");
        return "业务代码返回值";
    }
}
@SpringBootTest
class AopDemoApplicationTests {

    @Autowired
    private MyService myService;

    @Test
    void contextLoads() {
        System.out.println(myService.hello());
    }

}

springboot aop 通知执行顺序(基于jdk1.8.0_341)_第3张图片

有返回值&有异常:

@Service
public class MyService {
    public String hello(){
        System.out.println("业务代码1");
        int i=1/0;
        System.out.println("业务代码2");
        return "业务代码返回值";
    }
}
@SpringBootTest
class AopDemoApplicationTests {

    @Autowired
    private MyService myService;

    @Test
    void contextLoads() {
        System.out.println(myService.hello());
    }
}

springboot aop 通知执行顺序(基于jdk1.8.0_341)_第4张图片

你可能感兴趣的:(aop,java)