Recall & Review
beginner
What does the @After annotation do in Spring AOP?
The @After annotation runs advice after a method finishes, no matter if it returns normally or throws an exception.
Click to reveal answer
beginner
How is @AfterReturning different from @After in Spring AOP?
@AfterReturning runs advice only after a method completes successfully (returns without exception). It can access the returned value.
Click to reveal answer
intermediate
Show a simple example of @AfterReturning advice that logs the returned value.
@AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result")
public void logReturnValue(Object result) {
System.out.println("Returned: " + result);
}
Click to reveal answer
intermediate
When would you use @After instead of @AfterReturning?
Use @After when you want to run code after a method finishes regardless of success or failure, like cleaning up resources.
Click to reveal answer
advanced
Can @AfterReturning advice modify the returned value before it reaches the caller?
No, @AfterReturning can read the returned value but cannot change it. To modify return values, use @Around advice.
Click to reveal answer
What happens if a method throws an exception? Which advice runs?
✗ Incorrect
@After runs after method completion no matter what. @AfterReturning runs only if the method returns normally.
Which annotation lets you access the method's return value?
✗ Incorrect
@AfterReturning can capture the returned value using the 'returning' attribute.
If you want to run code after a method no matter what, which advice do you choose?
✗ Incorrect
@After advice runs after method execution regardless of outcome.
Can @AfterReturning advice change the method's return value?
✗ Incorrect
@AfterReturning advice can read but not modify the returned value.
Which advice type would you use to modify the return value?
✗ Incorrect
@Around advice can control and modify the method execution and its return value.
Explain the difference between @After and @AfterReturning in Spring AOP.
Think about what happens if the method throws an error.
You got /3 concepts.
Describe a real-life scenario where you would use @After advice instead of @AfterReturning.
Consider what happens if a method fails but you still want to do something.
You got /3 concepts.