Q1. What is a pointcut expression?
A pointcut expression is a language that matches joinpoints. In Spring AOP, it uses AspectJ pointcut expressions. Common designators: execution (method execution), within (within certain types), args (argument types), @annotation (annotated methods), etc.
Q2. Write a pointcut expression to match all methods in a package.
Example:
@Pointcut("execution(* com.example.service.*.*(..))")
public void serviceLayer() {}
This matches any method in any class in com.example.service package.Q3. How do you match methods with a specific annotation?
Use @annotation designator. Example:
@Pointcut("@annotation(com.example.Loggable)")
public void loggableMethods() {}
This matches any method annotated with @Loggable.Q4. What is the difference between execution and within pointcut designators?
execution matches method execution joinpoints based on signature (return type, class, method, parameters). within matches joinpoints within certain types (classes). For example, within(com.example.service.*) matches all methods in classes of that package, but execution gives more fine-grained control over method signatures.
Q5. Can you combine pointcut expressions?
Yes, you can combine pointcuts using &&, ||, and ! operators. Example:
@Pointcut("execution(* *.*(..)) && @annotation(Cacheable)")
public void cacheableMethods() {}
