Challenge - 5 Problems
Spring AOP After Advice Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Behavior of @After advice in Spring AOP
Consider a Spring AOP aspect with an @After advice on a method that throws an exception. What will happen when the advised method throws an exception?
Spring Boot
public class MyAspect { @After("execution(* com.example.service.*.*(..))") public void afterAdvice() { System.out.println("After advice executed"); } } // The target method throws a RuntimeException.
Attempts:
2 left
💡 Hint
Think about whether @After runs regardless of method outcome.
✗ Incorrect
The @After advice runs after the method execution regardless of its outcome, whether it returns normally or throws an exception.
❓ component_behavior
intermediate2:00remaining
Return value availability in @AfterReturning advice
In Spring AOP, which of the following is true about the return value in an @AfterReturning advice?
Spring Boot
public class MyAspect { @AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result") public void afterReturningAdvice(Object result) { System.out.println("Returned: " + result); } }
Attempts:
2 left
💡 Hint
Consider when a method actually returns a value.
✗ Incorrect
@AfterReturning advice runs only if the method completes successfully and provides the return value as a parameter.
📝 Syntax
advanced2:00remaining
Correct syntax for @AfterReturning with returning attribute
Which of the following @AfterReturning advice declarations is syntactically correct in Spring AOP?
Attempts:
2 left
💡 Hint
Check the attribute name and parameter matching.
✗ Incorrect
The attribute name must be 'returning' as a string, and the advice method must have a parameter matching that name.
🔧 Debug
advanced2:00remaining
Why does @AfterReturning advice not execute?
Given this aspect code, why does the @AfterReturning advice never print its message?
public class MyAspect {
@AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result")
public void afterReturningAdvice(String result) {
System.out.println("Returned: " + result);
}
}
The target method returns an Integer.
Attempts:
2 left
💡 Hint
Check the parameter type of the advice method compared to the return type.
✗ Incorrect
The advice method parameter type must be compatible with the actual return type. Mismatch causes advice not to run.
❓ lifecycle
expert3:00remaining
Order of execution: @After, @AfterReturning, and exception thrown
Consider a Spring AOP aspect with both @After and @AfterReturning advices on the same method. The method throws an exception. Which advice(s) will execute?
Spring Boot
public class MyAspect { @After("execution(* com.example.service.*.*(..))") public void afterAdvice() { System.out.println("After advice executed"); } @AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result") public void afterReturningAdvice(Object result) { System.out.println("AfterReturning advice executed with: " + result); } }
Attempts:
2 left
💡 Hint
Recall when each advice runs relative to exceptions.
✗ Incorrect
@After advice runs always after method execution, even if an exception occurs. @AfterReturning runs only if the method returns normally.