0
0
Spring Bootframework~5 mins

@After and @AfterReturning in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A@After advice runs, but @AfterReturning does not.
BBoth @After and @AfterReturning run.
COnly @AfterReturning runs.
DNeither advice runs.
Which annotation lets you access the method's return value?
A@After
B@Before
C@AfterReturning
D@Around
If you want to run code after a method no matter what, which advice do you choose?
A@Around
B@AfterReturning
C@Before
D@After
Can @AfterReturning advice change the method's return value?
ANo, it can only read the value.
BOnly if combined with @Before.
COnly if the method is void.
DYes, always.
Which advice type would you use to modify the return value?
A@After
B@Around
C@AfterReturning
D@Before
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.