Java Reference
In-Depth Information
package com.apress.springenterpriserecipes.calculator;
...
import java.util.Arrays;
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,
IllegalArgumentException e) {
log.error ("Illegal argument " + Arrays.toString(joinPoint.getArgs())
+ " in " + joinPoint.getSignature().getName() + "()");
}
}
Around Advices
The last type of advice is an around advice. It is the most powerful of all the advice types. It gains full
control of a join point, so you can combine all the actions of the preceding advices into one single
advice. You can even control when, and whether, to proceed with the original join point execution.
The following around advice is the combination of the before, after returning, and after throwing
advices you created before. Note that for an around advice, the argument type of the join point must be
ProceedingJoinPoint . It's a subinterface of JoinPoint that allows you to control when to proceed with
the original join point.
package com.apress.springenterpriserecipes.calculator;
...
import java.util.Arrays;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@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 "
Search WWH ::




Custom Search