Java Reference
In-Depth Information
ArithmeticCalculator arithmeticCalculator =
(ArithmeticCalculator) context.getBean("arithmeticCalculator");
arithmeticCalculator.add(1, 2);
arithmeticCalculator.sub(4, 3);
arithmeticCalculator.mul(2, 3);
arithmeticCalculator.div(4, 2);
UnitCalculator unitCalculator =
(UnitCalculator) context.getBean("unitCalculator");
The execution points matched by a pointcut are called join points . In this term, a pointcut is an
expression to match a set of join points, while an advice is the action to take at a particular join point.
For your advice to access the detail of the current join point, you can declare an argument of type
JoinPoint in your advice method. Then you can get access to join point details such as the method name
and argument values. Now you can expand your pointcut to match all methods by changing the class
name and method name to wildcards.
package com.apress.springenterpriserecipes.calculator;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
public class CalculatorLoggingAspect {
@Before("execution(* *.*(..))")
public void logBefore( JoinPoint joinPoint) {"The method " + joinPoint.getSignature().getName()
+ "() begins with " + Arrays.toString(joinPoint.getArgs()));
After Advices
An after advice is executed after a join point finishes, whenever it returns a result or throws an exception
abnormally. The following after advice logs the calculator method ending. An aspect may include one or
more advices.
package com.apress.springenterpriserecipes.calculator;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
Search WWH ::

Custom Search