Bird
0
0

Why does this Around advice cause a StackOverflowError?

medium📝 Troubleshoot Q7 of 15
Spring Boot - Aspect-Oriented Programming
Why does this Around advice cause a StackOverflowError?
@Around("execution(* com.example.service.*.*(..))")
public Object monitor(ProceedingJoinPoint pjp) throws Throwable {
  long start = System.currentTimeMillis();
  Object result = monitor(pjp);
  long end = System.currentTimeMillis();
  System.out.println("Time: " + (end - start));
  return result;
}
AThe pointcut expression is incorrect
BThe method should return void
CIt does not handle exceptions properly
DIt recursively calls itself instead of pjp.proceed()
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method call inside advice

    The advice calls monitor(pjp) inside itself, causing infinite recursion.
  2. Step 2: Correct call should be pjp.proceed()

    To execute target method, pjp.proceed() must be called, not the advice method itself.
  3. Final Answer:

    It recursively calls itself instead of pjp.proceed() -> Option D
  4. Quick Check:

    Recursive call in advice = StackOverflowError [OK]
Quick Trick: Call pjp.proceed(), not advice method, to avoid recursion [OK]
Common Mistakes:
  • Calling advice method recursively
  • Misusing pointcut expression
  • Ignoring exception handling

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes