Complex Pointcut
To create complex pointcuts, you can combine these designators using logical operators like && (and), || (or), and ! (not). Here are some examples:
Example 1: Combining execution and within
@Pointcut(value = "execution(* com.ayah.aop.service.*.*(..)) && within(com.ayah.aop.service.MyService)")
public void executionInMyService() {
}
- Explanation: This pointcut matches any method execution within classes in the
com.ayah.aop.servicepackage, but only if the class isMyService.
Example 2: Combining @annotation and args
@Pointcut(value = "@annotation(com.ayah.aop.annotation.Test) && args(param)")
public void testAnnotationWithParam(Object param) {
}
- Explanation: This pointcut matches methods annotated with
@Testand where the method has a parameter (captured asparam).
Example 3: Combining within and target
@Pointcut(value = "within(com.ayah.aop.service.*) && target(com.ayah.aop.service.AspectService)")
public void withinServiceTarget() {
}
- Explanation: This pointcut matches join points within any class in the
com.ayah.aop.servicepackage, but only if the target object is of typeAspectService.
Example 4: Using Logical Operators
@Pointcut(value = "execution(* com.ayah.aop.service.*.*(..)) && !within(com.ayah.aop.service.ExcludeService)")
public void executionNotInExcludeService() {
}
- Explanation: This pointcut matches all method executions within
com.ayah.aop.servicepackage, except those within theExcludeServiceclass.
Creating a Complex Pointcut
Here’s an example of a more complex pointcut that combines several conditions:
@Pointcut(value = "(execution(* com.ayah.aop.service.*.*(..)) || execution(* com.ayah.aop.other.*.*(..))) && @annotation(com.ayah.aop.annotation.TrackTime) && args(param)")
public void complexPointcut(Object param) {
}
- Explanation: This pointcut matches methods that:
- Are executed in classes within the
com.ayah.aop.serviceorcom.ayah.aop.otherpackages. - Are annotated with
@TrackTime. - Have parameters (captured as
param).
- Are executed in classes within the