Bird
Raised Fist0
Spring Bootframework~20 mins

@After and @AfterReturning in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Spring AOP After Advice Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2: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.
AThe @After advice will not execute if the method throws an exception.
BThe @After advice will execute even if the method throws an exception.
CThe @After advice will execute only if the method returns normally without exception.
DThe @After advice will execute before the method is called.
Attempts:
2 left
💡 Hint
Think about whether @After runs regardless of method outcome.
❓ component_behavior
intermediate
2: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);
    }
}
AThe return value is always null in @AfterReturning advice.
BThe return value is available even if the method throws an exception.
CThe return value is available only if the method completes normally without exception.
DThe return value is available before the method executes.
Attempts:
2 left
💡 Hint
Consider when a method actually returns a value.
📝 Syntax
advanced
2:00remaining
Correct syntax for @AfterReturning with returning attribute
Which of the following @AfterReturning advice declarations is syntactically correct in Spring AOP?
A
@AfterReturning(pointcut = "execution(* com.example.*.*(..))", returning = retVal)
public void adviceMethod(Object retVal) {}
B
@AfterReturning(pointcut = "execution(* com.example.*.*(..))", return = "retVal")
public void adviceMethod(Object retVal) {}
C
@AfterReturning(pointcut = "execution(* com.example.*.*(..))", returning = "retVal")
public void adviceMethod() {}
D
@AfterReturning(pointcut = "execution(* com.example.*.*(..))", returning = "retVal")
public void adviceMethod(Object retVal) {}
Attempts:
2 left
💡 Hint
Check the attribute name and parameter matching.
🔧 Debug
advanced
2: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.
AThe advice method parameter type String does not match the actual return type Integer.
BThe pointcut expression is incorrect and does not match the method.
CThe @AfterReturning advice only works for void methods.
DThe advice method must have no parameters.
Attempts:
2 left
💡 Hint
Check the parameter type of the advice method compared to the return type.
❓ lifecycle
expert
3: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);
    }
}
AOnly the @After advice executes; @AfterReturning does not execute because of the exception.
BNeither advice executes because the method threw an exception.
COnly the @AfterReturning advice executes; @After does not execute.
DBoth @After and @AfterReturning advices execute regardless of exception.
Attempts:
2 left
💡 Hint
Recall when each advice runs relative to exceptions.

Practice

(1/5)
1. What is the main difference between @After and @AfterReturning in Spring AOP?
easy
A. @After runs after method completion regardless of outcome; @AfterReturning runs only after successful return.
B. @After runs before the method; @AfterReturning runs after the method.
C. @After runs only if the method throws an exception; @AfterReturning runs always.
D. @After and @AfterReturning are exactly the same.

Solution

  1. Step 1: Understand @After behavior

    @After advice runs after a method finishes, whether it returns normally or throws an exception.
  2. Step 2: Understand @AfterReturning behavior

    @AfterReturning advice runs only if the method completes successfully without throwing exceptions.
  3. Final Answer:

    @After runs after method completion regardless of outcome; @AfterReturning runs only after successful return. -> Option A
  4. Quick Check:

    @After = always after, @AfterReturning = after success [OK]
Hint: Remember: @After always runs, @AfterReturning only on success [OK]
Common Mistakes:
  • Confusing @After with @Before advice
  • Thinking @AfterReturning runs on exceptions
  • Believing both advices behave identically
2. Which of the following is the correct syntax to define an @AfterReturning advice method in Spring AOP?
easy
A. @AfterReturning(pointcut = "execution(* com.example.*.*(..))", returning = "result") public void afterReturn(Object result) { }
B. @AfterReturning("execution(* com.example.*.*(..))") public void afterReturn() { }
C. @AfterReturning(returning = "result") public void afterReturn(Object result) { }
D. @AfterReturning(pointcut = "execution(* com.example.*.*(..))") public void afterReturn(Object result) { }

Solution

  1. Step 1: Check required attributes for @AfterReturning

    The @AfterReturning annotation requires a pointcut expression and optionally a returning attribute to capture the returned value.
  2. Step 2: Match method signature with returning attribute

    The method must have a parameter matching the name in returning to receive the returned value.
  3. Final Answer:

    @AfterReturning(pointcut = "execution(* com.example.*.*(..))", returning = "result") public void afterReturn(Object result) { } -> Option A
  4. Quick Check:

    Use both pointcut and returning with matching parameter [OK]
Hint: Use both pointcut and returning with matching method parameter [OK]
Common Mistakes:
  • Omitting pointcut attribute
  • Not matching returning name with method parameter
  • Using @AfterReturning without parameters
3. Given the following Spring AOP advice, what will be printed if someMethod() throws an exception?
@After("execution(* com.example.Service.someMethod(..))")
public void afterAdvice() {
    System.out.println("After advice executed");
}

@AfterReturning(pointcut = "execution(* com.example.Service.someMethod(..))", returning = "result")
public void afterReturningAdvice(Object result) {
    System.out.println("AfterReturning advice executed with: " + result);
}
medium
A. No advice executed
B. AfterReturning advice executed with: null
C. Both advices executed
D. After advice executed

Solution

  1. Step 1: Analyze @After advice behavior on exception

    @After advice runs after method finishes regardless of success or exception, so it will execute and print its message.
  2. Step 2: Analyze @AfterReturning advice behavior on exception

    @AfterReturning advice runs only if the method returns successfully, so it will NOT run if an exception is thrown.
  3. Final Answer:

    After advice executed -> Option D
  4. Quick Check:

    @After runs always, @AfterReturning skips on exceptions [OK]
Hint: @After runs always; @AfterReturning skips exceptions [OK]
Common Mistakes:
  • Assuming @AfterReturning runs on exceptions
  • Thinking both advices run always
  • Confusing advice execution order
4. Identify the error in the following advice method:
@AfterReturning(pointcut = "execution(* com.example.Service.process(..))", returning = "retVal")
public void afterReturn() {
    System.out.println("Returned value: " + retVal);
}
medium
A. The method should be annotated with @After instead of @AfterReturning.
B. The pointcut expression is invalid.
C. The advice method must have a parameter named retVal to receive the returned value.
D. The method must return a value.

Solution

  1. Step 1: Check method parameters for returning attribute

    The returning = "retVal" attribute requires the advice method to have a parameter named retVal to receive the returned value.
  2. Step 2: Verify method signature

    The method currently has no parameters, so it cannot access retVal, causing a compile error.
  3. Final Answer:

    The advice method must have a parameter named retVal to receive the returned value. -> Option C
  4. Quick Check:

    Returning attribute needs matching method parameter [OK]
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

  1. 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.
  2. 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.
  3. Final Answer:

    Use @AfterReturning to log the returned value and @After to log after method completion. -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard