關於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.  
    }

  測試的結果是:

技術分享

這樣就很清楚的看出各種方法是在什么時候調用的啦


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM