Java Reference
In-Depth Information
public void logAfterReturning(JoinPoint joinPoint, Object result) {
log.info("The method " + joinPoint.getSignature().getName()
+ "() ends with " + result);
}
}
After Throwing Advices
An after throwing advice is executed only when an exception is thrown by a join point.
package com.apress.springenterpriserecipes.calculator;
...
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class CalculatorLoggingAspect {
...
@AfterThrowing("execution(* *.*(..))")
public void logAfterThrowing(JoinPoint joinPoint) {
log.error("An exception has been thrown in "
+ joinPoint.getSignature().getName() + "()");
}
}
Similarly, the exception thrown by the join point can be accessed by adding a throwing attribute
to the @AfterThrowing annotation. The type Throwable is the superclass of all errors and exceptions in
the Java language. So the following advice will catch any of the errors and exceptions thrown by the
join points:
package com.apress.springenterpriserecipes.calculator;
...
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
@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() + "()");
}
}
However, if you are interested in one particular type of exception only, you can declare it as the
argument type of the exception. Then your advice will be executed only when exceptions of compatible
type (i.e., this type and its subtypes) are thrown.
Search WWH ::




Custom Search