Spring Boot Aspect-Oriented Programming 2 — Questions and Answers
Question 1: Which Spring AOP advice type runs only when a method throws an exception?
- @Before
- @AfterReturning
- @AfterThrowing (Correct answer)
- @Around
Correct answer: @AfterThrowing
@AfterThrowing advice executes only when the advised method exits by throwing an exception.
Question 2: What object does @Around advice receive as its parameter to control method execution?
- JoinPoint
- ProceedingJoinPoint (Correct answer)
- MethodSignature
- AspectContext
Correct answer: ProceedingJoinPoint
@Around advice must declare a ProceedingJoinPoint parameter, which provides the proceed() method to invoke the target.
Question 3: In a Spring Boot project, what triggers auto-configuration of AOP support?
- Adding @EnableAspectJAutoProxy manually
- Including spring-boot-starter-aop on the classpath (Correct answer)
- Defining a @Configuration class
- Setting spring.aop.enabled=true in application.properties
Correct answer: Including spring-boot-starter-aop on the classpath
spring-boot-starter-aop pulls in the AspectJ weaver and auto-configures AOP proxying without any extra annotation.
Question 4: What does the 'within' PCD match on?
- Method return types
- Types (classes or packages) containing join points (Correct answer)
- Method parameter types
- Annotation values at runtime
Correct answer: Types (classes or packages) containing join points
The within() pointcut designator limits matching to join points inside a specified type or package.
Question 5: How can you pass the thrown exception object into an @AfterThrowing advice method?
- Use a JoinPoint parameter
- Declare a Throwable parameter with the same name as the 'throwing' attribute (Correct answer)
- Autowire ExceptionContext
- Use @ExceptionHandler inside the aspect
Correct answer: Declare a Throwable parameter with the same name as the 'throwing' attribute
Setting the 'throwing' attribute on @AfterThrowing and declaring a matching parameter binds the thrown exception to that parameter.
Question 6: Which of the following is a limitation of Spring AOP compared to full AspectJ weaving?
- Spring AOP cannot use @Aspect annotations
- Spring AOP only intercepts Spring-managed bean method calls (Correct answer)
- Spring AOP cannot use pointcut expressions
- Spring AOP requires XML configuration
Correct answer: Spring AOP only intercepts Spring-managed bean method calls
Spring AOP uses JDK/CGLIB proxies, so it can only intercept calls going through the Spring proxy on managed beans.
Question 7: What is the purpose of @DeclareParents in Spring AOP?
- To declare a parent aspect
- To introduce new interfaces and default implementations to existing beans (Correct answer)
- To set the order of aspect execution
- To mark a class as a pointcut library
Correct answer: To introduce new interfaces and default implementations to existing beans
@DeclareParents enables introduction, adding new interface implementations to existing target objects without modifying them.
Which Spring AOP advice type runs only when a method throws an exception?