Spring Boot Aspect-Oriented Programming 4 — Questions and Answers
Question 1: Which PCD matches join points where the subject has a given annotation at the type level?
- @annotation
- @within (Correct answer)
- @args
- @target
Correct answer: @within
@within() matches join points in types that carry the specified annotation, whereas @annotation() matches methods carrying it directly.
Question 2: What is a 'weaving' phase in AOP terminology?
- Compiling aspect source code
- Linking aspects with application types to create advised objects (Correct answer)
- Packaging aspects into a JAR
- Running unit tests for aspects
Correct answer: Linking aspects with application types to create advised objects
Weaving is the process of applying aspects to a target object, which can happen at compile time, load time, or runtime (as in Spring AOP).
Question 3: How do you bind a method's return value inside an @AfterReturning advice?
- Declare a parameter named 'result' automatically
- Set the 'returning' attribute on @AfterReturning to match a method parameter name (Correct answer)
- Use JoinPoint.getReturnValue()
- Annotate the parameter with @ReturnValue
Correct answer: Set the 'returning' attribute on @AfterReturning to match a method parameter name
The 'returning' attribute of @AfterReturning must match the advice method parameter name where the return value will be injected.
Question 4: Which of the following correctly combines two pointcut expressions with a logical AND?
- execution(* *.*(..)) | within(com.example.*)
- execution(* *.*(..)) && within(com.example.*) (Correct answer)
- execution(* *.*(..)) AND within(com.example.*)
- execution(* *.*(..)) + within(com.example.*)
Correct answer: execution(* *.*(..)) && within(com.example.*)
Pointcut expressions use Java-style && (and ||, !) for logical composition inside @Pointcut and advice annotations.
Question 5: In Spring AOP, which join point type is supported?
- Field access
- Constructor execution
- Method execution (Correct answer)
- Static initializer execution
Correct answer: Method execution
Spring AOP only supports method execution join points; field access and constructor join points require full AspectJ weaving.
Question 6: What is the effect of calling ProceedingJoinPoint.proceed(newArgs) in @Around advice?
- Replaces the target method entirely
- Invokes the target method with substituted arguments (Correct answer)
- Retries the method call indefinitely
- Suppresses the return value
Correct answer: Invokes the target method with substituted arguments
proceed(Object[] args) lets @Around advice invoke the target method with a different set of arguments than it originally received.
Question 7: Which annotation makes a class eligible to be detected as an aspect by Spring's component scan?
- @Aspect alone
- @Aspect combined with @Component (or another stereotype) (Correct answer)
- @Pointcut
- @AdviceType
Correct answer: @Aspect combined with @Component (or another stereotype)
@Aspect marks the class as an aspect, but @Component (or @Bean in a config class) is needed for Spring to detect and register it as a bean.
Which PCD matches join points where the subject has a given annotation at the type level?