Bird
0
0

Given this Spring Boot aspect code snippet:

medium📝 component behavior Q13 of 15
Spring Boot - Aspect-Oriented Programming
Given this Spring Boot aspect code snippet:
@Aspect
public class LoggingAspect {
  @Before("execution(* com.example.service.*.*(..))")
  public void logBefore() {
    System.out.println("Method called");
  }
}

What will happen when a method in com.example.service package is called?
AThe message "Method called" will print after the method executes
BThe message "Method called" will print before the method executes
CNo message will print because the pointcut is incorrect
DThe method call will be blocked and not executed
Step-by-Step Solution
Solution:
  1. Step 1: Understand the @Before advice

    The @Before annotation means the method logBefore() runs before the matched method executes.
  2. Step 2: Analyze the pointcut expression

    The pointcut execution(* com.example.service.*.*(..)) matches any method in any class inside com.example.service package.
  3. Final Answer:

    The message "Method called" will print before the method executes -> Option B
  4. Quick Check:

    @Before advice prints message before method [OK]
Quick Trick: @Before runs code before matched methods [OK]
Common Mistakes:
  • Confusing @Before with @After advice
  • Thinking pointcut matches other packages
  • Assuming method is blocked

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes