Hint: Match returning name with method parameter to avoid errors [OK]
Common Mistakes:
Omitting method parameter for returning value
Misnaming the parameter
Using @AfterReturning without pointcut
5. You want to log the returned value of a service method only when it completes successfully, and also log a message after the method finishes regardless of outcome. Which advice combination should you use?
hard
A. Use two @AfterReturning advices for both logs.
B. Use @AfterReturning to log the returned value and @After to log after method completion.
C. Use @After advice only for both logs.
D. Use @Before advice to log returned value and @AfterReturning to log after completion.
Solution
Step 1: Log returned value only on success
@AfterReturning advice runs only after successful method return, so it is suitable to log the returned value.
Step 2: Log after method finishes regardless of outcome
@After advice runs after method completion whether success or exception, so it is suitable to log a message always.
Final Answer:
Use @AfterReturning to log the returned value and @After to log after method completion. -> Option B
Quick Check:
AfterReturning = success only, After = always after [OK]
Hint: Combine @AfterReturning for success and @After for always [OK]
Common Mistakes:
Using @After for returned value logging
Using @Before to log returned value
Expecting @AfterReturning to run on exceptions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.