Java Reference
In-Depth Information
@Pointcut("within(UnitCalculator+)")
public void unitOperation() {}
@Pointcut(" arithmeticOperation() || unitOperation()")
public void loggingOperation() {}
}
Declaring Pointcut Parameters
One way to access join point information is by reflection (i.e., via an argument of type
org.aspectj.lang.JoinPoint in the advice method). Besides, you can access join point information
in a declarative way by using some kinds of special pointcut expressions. For example, the expressions
target() and args() capture the target object and argument values of the current join point and expose
them as pointcut parameters. These parameters will be passed to your advice method via arguments of
the same name.
package com.apress.springenterpriserecipes.calculator;
...
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class CalculatorLoggingAspect {
...
@Before("execution(* *.*(..)) && target(target) && args(a,b)")
public void logParameter( Object target, double a, double b) {
log.info("Target class : " + target.getClass().getName());
log.info("Arguments : " + a + ", " + b);
}
}
When declaring an independent pointcut that exposes parameters, you have to include them in the
argument list of the pointcut method as well.
package com.apress.springenterpriserecipes.calculator;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class CalculatorPointcuts {
...
@Pointcut("execution(* *.*(..)) && target(target) && args(a,b)")
public void parameterPointcut( Object target, double a, double b) {}
}
Any advice that refers to this parameterized pointcut can access the pointcut parameters via
method arguments of the same name.
package com.apress.springenterpriserecipes.calculator;
...
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
Search WWH ::




Custom Search