Java Reference
In-Depth Information
@Aspect
public class CalculatorLoggingAspect {
...
@After("execution(* *.*(..))")
public void logAfter(JoinPoint joinPoint) {
log.info("The method " + joinPoint.getSignature().getName()
+ "() ends");
}
}
After Returning Advices
An after advice is executed regardless of whether a join point returns normally. If you want to perform
logging only when a join point returns, you should replace the after advice with an after returning advice.
package com.apress.springenterpriserecipes.calculator;
...
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class CalculatorLoggingAspect {
...
@AfterReturning("execution(* *.*(..))")
public void logAfterReturning(JoinPoint joinPoint) {
log.info("The method " + joinPoint.getSignature().getName()
+ "() ends");
}
}
In an after returning advice, you can get access to the return value of a join point by adding a
returning attribute to the @AfterReturning annotation. The value of this attribute should be the
argument name of this advice method for the return value to pass in. Then you have to add an argument
to the advice method signature with this name. At runtime, Spring AOP will pass in the return value
through this argument. Also note that the original pointcut expression needs to be presented in the
pointcut attribute instead.
package com.apress.springenterpriserecipes.calculator;
...
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class CalculatorLoggingAspect {
...
@AfterReturning(
pointcut = "execution(* *.*(..))",
returning = "result")
Search WWH ::




Custom Search