Spring-AOP通知获取数据

推荐先阅读Spring全家桶

获取切入点方法的参数:所有通知

  • JoinPoint:适用于@Before@After@AfterReturning@AfterThrowing

    // JoinPoint对象描述了连接点方法的运行状态,可以获取到原始方法的调用参数
    @Before("pt()")
    public void before(JoinPoint jp){
    Object[] args = jp.getArgs();
    System.out.println(Arrays.toString(args));
    }
  • ProceedingJoinPoint:适用于@Around

    // ProceedJointPoint是JoinPoint的子类
    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp)throws Throwable {
    Object[] args = pjp.getArgs();
    System.out.println(Arrays.toString(args));
    Object ret = pjp.proceed();
    return ret;
    }

获取切入点方法返回值:@Before@AfterThrowing是没有返回值,@After可有可无,所以不做研究

  • @AfterReturning

    @AfterReturning(value = "pt()",returning = "ret")
    public void afterReturning(Object ret) {
    System.out.println("afterReturning advice ..."+ret);
    }
  • @Around

    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
    Object ret = pjp.proceed();
    return ret;
    }

获取切入点方法运行异常信息:@Before@AfterReturning是不会有,@After可有可无,所以不做研究

  • @AfterThrowing

    @AfterThrowing(value = "pt()",throwing = "t")
    public void afterThrowing(Throwable t){
    System.out.println("afterThrowing advice ..." + t);
    }
  • @Around

    @Around("pt()")
    public Object around(ProceedingJointPoint pjp){
    Object ret = null;
    try {
    ret = pjp.proceed();
    } catch (Throwable t) {
    t.printStackTrace();
    }
    return ret;
    }