《Spring Recipes》第三章筆記2:Declaring Aspects

 《Spring Recipes》第三章筆記2:Declaring Aspects


Spring容器支持的AspectJ註解:@Aspect,@Before,@After,@AfterReturning,@AfterThrowing,@Around。
注意:切面必須讓Spring容器管理(使用註解或者在配置文件中聲明切面)才能生效。

一、@Aspect:聲明一個切面。
二、@Before:定義在切入點以前執行特定代碼。
@Aspect
public class CalculatorLoggingAspect {
  private Log log = LogFactory.getLog(this.getClass());
  @Before("execution(* ArithmeticCalculator.add(..))")
  public void logBefore() {
    log.info("The method add() begins");
  }
}

能夠通知方法中添加JoinPoint類型的參數,獲取JoinPoint的信息:
@Aspect
public class CalculatorLoggingAspect {
... ...
  @Before("execution(* *.*(..))")
  public void logBefore(JoinPoint joinPoint) {
    log.info("The method " + joinPoint.getSignature().getName()+ "() begins with " + Arrays.toString(joinPoint.getArgs()));
  }
}


三、@After:定義在切入點以後 執行特定代碼,不管切入點是成功返回仍是拋出異常。
@Aspect
public class CalculatorLoggingAspect {
...
  @After("execution(* *.*(..))")
  public void logAfter(JoinPoint joinPoint) {
    log.info("The method " + joinPoint.getSignature().getName()
    + "() ends");
  }
}


四、@AfterReturning:定義在切入點成功返回後執行特定代碼。
@Aspect
public class CalculatorLoggingAspect {
... ...
  @AfterReturning("execution(* *.*(..))")
  public void logAfterReturning(JoinPoint joinPoint) {
  log.info("The method " + joinPoint.getSignature().getName()+ "() ends");
  }
}

能夠在@ AfterReturning註解中添加returning,在通知中添加Object類型的參數獲取方法的返回信息:
@Aspect
public class CalculatorLoggingAspect {
... ...
  @AfterReturning(pointcut = "execution(* *.*(..))",
                  returning = "result")
  public void logAfterReturning(JoinPoint joinPoint, Object result) {
    log.info("The method " + joinPoint.getSignature().getName()+ "() ends with " + result);
  }
}


五、@AfterThrowing:定義在切入點拋出異常後執行特定代碼,能夠指定拋出的異常類型。
@Aspect
public class CalculatorLoggingAspect {
... ...
  @AfterThrowing("execution(* *.*(..))")
  public void logAfterThrowing(JoinPoint joinPoint) {
    log.error("An exception has been thrown in "+ joinPoint.getSignature().getName() + "()");
  }
}

能夠經過在 @AfterThrowing 中添加throwing獲取異常,在通知中添加Throwable類型的參數:
@Aspect
public class CalculatorLoggingAspect {
... ...
  @AfterThrowing(pointcut = "execution(* *.*(..))",
                 throwing = "e")
  public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
    log.error("An exception " + e + " has been thrown in "+ joinPoint.getSignature().getName() + "()");
  }
}

能夠經過在通知中指定異常類型,來只捕獲特定類型的異常:
@Aspect
public class CalculatorLoggingAspect {
... ...
  @AfterThrowing(pointcut = "execution(* *.*(..))",
                 throwing = "e")
  public void logAfterThrowing(JoinPoint joinPoint,IllegalArgumentException e) {
    log.error("Illegal argument " + Arrays.toString(joinPoint.getArgs())+ " in " + joinPoint.getSignature().getName() + "()");
  }
}


六、@Around:對切入點具備徹底的控制,能夠在一個通知中定義上面5中類型的通知。
@Around通知的參數必須爲ProceedingJoinPoint類型。
@Aspect
public class CalculatorLoggingAspect {
...
@Around("execution(* *.*(..))")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
  log.info("The method " + joinPoint.getSignature().getName()+ "() begins with " + Arrays.toString(joinPoint.getArgs()));
  try {
    Object result = joinPoint.proceed();
    log.info("The method " + joinPoint.getSignature().getName()+ "() ends with " + result);
    return result;
   } catch (IllegalArgumentException e) {
    log.error("Illegal argument "+ Arrays.toString(joinPoint.getArgs()) + " in "+ joinPoint.getSignature().getName() + "()");
    throw e;
   }
 }
}
相關文章
相關標籤/搜索