Bird
0
0

Identify the error in this Spring AOP logging advice code:

medium📝 Troubleshoot Q14 of 15
Spring Boot - Aspect-Oriented Programming
Identify the error in this Spring AOP logging advice code:
@Before("execution(* com.example..*(..))")
public void logStart() {
    System.out.println("Starting method");
}

@AfterReturning("execution(* com.example.service.*.*(..))")
public void logEnd() {
    System.out.println("Method ended");
}
AThe pointcut expressions target different packages
BThe method signatures lack return types
CThe @AfterReturning advice is missing a returning attribute
DThe pointcut expression in @Before is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Check pointcut expressions

    @Before targets "com.example..*" (all subpackages), @AfterReturning targets only "com.example.service.*".
  2. Step 2: Understand mismatch impact

    This mismatch means @Before runs on more methods than @AfterReturning, causing inconsistent logging.
  3. Final Answer:

    The pointcut expressions target different packages -> Option A
  4. Quick Check:

    Pointcut mismatch = A [OK]
Quick Trick: Match pointcut packages exactly for consistent logging [OK]
Common Mistakes:
  • Assuming pointcut syntax is wrong
  • Thinking return type is required in advice methods
  • Believing @AfterReturning must have returning attribute always

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes