Bird
0
0

You want to log a message before any method in com.example.repository package is called, but only for methods starting with save. Which @Before pointcut expression correctly matches this?

hard📝 Application Q15 of 15
Spring Boot - Aspect-Oriented Programming
You want to log a message before any method in com.example.repository package is called, but only for methods starting with save. Which @Before pointcut expression correctly matches this?
A@Before("execution(* com.example.repository.save*(..))")
B@Before("execution(* com.example.repository.*save*(..))")
C@Before("execution(* com.example.repository..save*(..))")
D@Before("execution(* com.example.repository.*.save*(..))")
Step-by-Step Solution
Solution:
  1. Step 1: Understand pointcut syntax for package, class, and method

    To match methods starting with 'save' in any class of the com.example.repository package, use execution(* com.example.repository.*.save*(..)). However, if you want to include subpackages as well, use '..' instead of a single '*'.
  2. Step 2: Analyze each option

    A matches only methods named save* in the package directly (no class wildcard), B is invalid syntax, C matches methods in any class in the package but not subpackages, D matches methods starting with save in the package and all subpackages.
  3. Final Answer:

    @Before("execution(* com.example.repository..save*(..))") -> Option C
  4. Quick Check:

    package..save*(..) = matches package and subpackages [OK]
Quick Trick: Use package..save* to include subpackages and methods starting with save [OK]
Common Mistakes:
  • Using incorrect wildcards or dots in method name
  • Matching subpackages unintentionally
  • Misplacing wildcards in pointcut expression

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes