Spring Boot Aspect-Oriented Programming 3 — Questions and Answers
Question 1: When two aspects apply to the same join point, which interface determines their execution order?
- Comparable
- Ordered (Correct answer)
- AspectPriority
- BeanPostProcessor
Correct answer: Ordered
Implementing the org.springframework.core.Ordered interface (or using @Order) on an aspect controls its precedence relative to other aspects.
Question 2: What does the 'args' PCD do in a pointcut expression?
- Matches methods that declare specific formal parameters by type (Correct answer)
- Retrieves the return value after execution
- Limits advice to annotated methods only
- Captures exception arguments
Correct answer: Matches methods that declare specific formal parameters by type
The args() PCD matches join points where the arguments passed at runtime are instances of the given types.
Question 3: In Spring AOP, what proxy type is used by default when the target class does not implement any interface?
- JDK dynamic proxy
- CGLIB subclass proxy (Correct answer)
- Javassist proxy
- Byte-buddy proxy
Correct answer: CGLIB subclass proxy
When no interface is available, Spring AOP falls back to CGLIB to create a subclass-based proxy of the target class.
Question 4: Which annotation would you place on a method inside an @Aspect class to reuse a pointcut expression across multiple advices?
- @Reusable
- @PointcutRef
- @Pointcut (Correct answer)
- @SharedAdvice
Correct answer: @Pointcut
@Pointcut annotates an empty method whose expression becomes a named, reusable pointcut referenced by advice annotations.
Question 5: What does 'proxyTargetClass = true' do in @EnableAspectJAutoProxy?
- Forces JDK dynamic proxies even for classes with interfaces
- Forces CGLIB proxying for all beans regardless of interface implementation (Correct answer)
- Disables AOP proxying globally
- Enables compile-time AspectJ weaving
Correct answer: Forces CGLIB proxying for all beans regardless of interface implementation
Setting proxyTargetClass=true forces Spring to use CGLIB subclass proxies for all beans, even those implementing interfaces.
Question 6: Which JoinPoint method returns the name of the method being advised?
- getTarget()
- getSignature().getName() (Correct answer)
- getArgs()[0]
- getThis().getClass().getName()
Correct answer: getSignature().getName()
JoinPoint.getSignature() returns a Signature object, and calling getName() on it gives the intercepted method's name.
Question 7: What happens if an @Around advice does NOT call ProceedingJoinPoint.proceed()?
- Spring automatically calls proceed on behalf of the advice
- The target method is never executed (Correct answer)
- An IllegalStateException is thrown
- The advice is skipped silently
Correct answer: The target method is never executed
If proceed() is never called inside @Around advice, the actual target method execution is suppressed entirely.
When two aspects apply to the same join point, which interface determines their execution order?