关于Aop切面中的@Before @Around等操作顺序的说明

【转】http://www.cnblogs.com/softidea/p/6123307.html

话不多说,直接上代码:

  1.  
    package com.cdms.aop.aspectImpl;
  2.  
     
  3.  
    import org.aspectj.lang.JoinPoint;
  4.  
    import org.aspectj.lang.ProceedingJoinPoint;
  5.  
    import org.aspectj.lang.annotation.*;
  6.  
    import org.springframework.stereotype.Component;
  7.  
     
  8.  
    import java.util.Arrays;
  9.  
    import java.util.List;
  10.  
     
  11.  
    /**
  12.  
    * 创建 by 草帽boy on 2017/4/1.
  13.  
    */
  14.  
    @Aspect
  15.  
    @Component
  16.  
    public class ICacheAopAction {
  17.  
    @Pointcut("@annotation(com.cdms.aop.ICache)")
  18.  
    private void controllerAspect(){}
  19.  
     
  20.  
    @Before("controllerAspect()")
  21.  
    public void Before(JoinPoint joinPoint){
  22.  
    String classname = joinPoint.getTarget().getClass().getSimpleName();
  23.  
    String methodName = joinPoint.getSignature().getName();
  24.  
    List<Object> args = Arrays.asList(joinPoint.getArgs());
  25.  
    System.out.println("@before Execute! --class name: " + classname + ", method name: " + methodName + " " + args );
  26.  
    }
  27.  
     
  28.  
    @Around("controllerAspect()")
  29.  
    public Object Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
  30.  
    System.out.println("@Around:执行目标方法之前...");
  31.  
    Object obj= proceedingJoinPoint.proceed();
  32.  
    System.out.println("@Around:执行目标方法之后...");
  33.  
    System.out.println("@Around:被织入的目标对象为:" + proceedingJoinPoint.getTarget());
  34.  
    System.out.println( "@Around:原返回值:" + obj + ",这是返回结果的后缀");
  35.  
    return obj;
  36.  
    }
  37.  
     
  38.  
    @AfterThrowing("controllerAspect()")
  39.  
    public void AfterThrowing(){
  40.  
    System.out.println("异常通知....");
  41.  
    }
  42.  
     
  43.  
    @After("controllerAspect()")
  44.  
    public void After(JoinPoint point){
  45.  
    System.out.println("@After:模拟释放资源...");
  46.  
    System.out.println("@After:目标方法为:" +
  47.  
    point.getSignature().getDeclaringTypeName() +
  48.  
    "." + point.getSignature().getName());
  49.  
    System.out.println("@After:参数为:" + Arrays.toString(point.getArgs()));
  50.  
    System.out.println("@After:被织入的目标对象为:" + point.getTarget());
  51.  
    }
  52.  
     
  53.  
    @AfterReturning("controllerAspect()")
  54.  
    public void AfterReturning(JoinPoint point){
  55.  
    System.out.println("@AfterReturning:模拟日志记录功能...");
  56.  
    System.out.println("@AfterReturning:目标方法为:" +
  57.  
    point.getSignature().getDeclaringTypeName() +
  58.  
    "." + point.getSignature().getName());
  59.  
    System.out.println("@AfterReturning:参数为:" +
  60.  
    Arrays.toString(point.getArgs()));
  61.  
    System.out.println("@AfterReturning:返回值为:" );
  62.  
    System.out.println("@AfterReturning:被织入的目标对象为:" + point.getTarget());
  63.  
    }
  64.  
     
  65.  
    }

  测试的结果是:

技术分享

这样就很清楚的看出各种方法是在什么时候调用的啦

posted @ 2019-12-11 12:48  LZ太热  阅读(1171)  评论(0编辑  收藏  举报