Spring Boot Aspect-Oriented Programming 5 — Questions and Answers
Question 1: What does the 'execution' PCD wildcard '..' mean when used in the parameter position?
- No parameters allowed
- Exactly one parameter of any type
- Any number of parameters of any type (Correct answer)
- Only primitive parameters
Correct answer: Any number of parameters of any type
In the parameter position of execution(), '..' matches zero or more parameters of any type.
Question 2: Which Spring AOP advice fires regardless of whether the method returns normally or throws an exception?
- @Before
- @AfterReturning
- @AfterThrowing
- @After (Correct answer)
Correct answer: @After
@After (finally) advice always runs after the method exits, whether by normal return or by throwing an exception.
Question 3: How can you restrict a pointcut to methods annotated with a custom annotation @Auditable?
- within(@Auditable *)
- @annotation(com.example.Auditable) (Correct answer)
- args(@Auditable)
- execution(@Auditable * *.*(..))
Correct answer: @annotation(com.example.Auditable)
@annotation(fully.qualified.AnnotationType) matches any method directly annotated with that annotation.
Question 4: What does 'target' PCD match on, as opposed to 'this'?
- 'target' matches the proxy object; 'this' matches the proxied target object
- 'target' matches the proxied target object; 'this' matches the proxy (Correct answer)
- 'target' and 'this' are interchangeable
- 'target' is for interfaces; 'this' is for classes
Correct answer: 'target' matches the proxied target object; 'this' matches the proxy
In Spring AOP, 'this' refers to the proxy bean, while 'target' refers to the actual target object being proxied.
Question 5: What is the correct return type for an @Around advice method that must return a value?
- void
- Object (Correct answer)
- Void
- ProceedingJoinPoint
Correct answer: Object
@Around advice must return Object so it can propagate whatever value proceed() returns (or a substituted value).
Question 6: Which scenario would cause Spring AOP advice NOT to fire despite a matching pointcut?
- Calling the advised method from an external bean via its Spring proxy
- Calling the advised method directly from within the same target class (self-invocation) (Correct answer)
- Calling the advised method through an @Autowired reference
- Calling the advised method from a different package
Correct answer: Calling the advised method directly from within the same target class (self-invocation)
Self-invocation bypasses the proxy, so the call goes directly to the target object and advice is never triggered.
Question 7: Which AspectJ-style pointcut expression matches all public methods in any class within the 'com.example.service' package and its sub-packages?
- execution(public * com.example.service.*.*(..))
- execution(public * com.example.service..*.*(..)) (Correct answer)
- within(com.example.service) && execution(public * *.*(..))
- execution(* com.example.service+.*(..))
Correct answer: execution(public * com.example.service..*.*(..))
The double dot (..) after the package name means 'this package and all sub-packages', so com.example.service..*.* covers all nested packages.
What does the 'execution' PCD wildcard '..' mean when used in the parameter position?