Bird
0
0

Identify the error in this aspect code:

medium📝 Debug Q14 of 15
Spring Boot - Aspect-Oriented Programming
Identify the error in this aspect code:
@Aspect
public class MyAspect {
  @Before("execution(* someMethod())")
  public void logBefore() {
    System.out.println("Before method");
  }

  public void someMethod() {
    System.out.println("Inside someMethod");
  }
}
AThe pointcut expression is incorrect
BThe aspect class must be annotated with @Component or registered as a bean
CThe advice method logBefore() must return a value
DThe someMethod() cannot be inside the aspect class
Step-by-Step Solution
Solution:
  1. Step 1: Check aspect registration

    In Spring Boot, an aspect class must be a Spring bean to work. It needs @Component or manual bean registration.
  2. Step 2: Validate other code parts

    The pointcut expression and advice method are correct. Having someMethod() inside the aspect is allowed but unusual.
  3. Final Answer:

    The aspect class must be annotated with @Component or registered as a bean -> Option B
  4. Quick Check:

    Aspect needs bean registration [OK]
Quick Trick: Aspect classes must be Spring beans to work [OK]
Common Mistakes:
  • Ignoring @Component or bean registration
  • Thinking advice methods must return values
  • Assuming pointcut syntax is wrong without checking

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes