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();
    }

}

无返回值&有异常:

@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();
    }
}

有返回值&无异常:

@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());
    }

}

有返回值&有异常:

@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());
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值