Bird
0
0

You want to measure execution time of a service method using @Around advice. Which code snippet correctly implements this?

hard📝 Application Q8 of 15
Spring Boot - Aspect-Oriented Programming
You want to measure execution time of a service method using @Around advice. Which code snippet correctly implements this?
A@Around("execution(* com.example.service.*.*(..))") public Object measureTime() throws Throwable { long start = System.currentTimeMillis(); Object result = pjp.proceed(); long end = System.currentTimeMillis(); System.out.println("Execution time: " + (end - start) + "ms"); return result; }
B@Around("execution(* com.example.service.*.*(..))") public void measureTime(ProceedingJoinPoint pjp) throws Throwable { long start = System.nanoTime(); pjp.proceed(); long end = System.nanoTime(); System.out.println("Execution time: " + (end - start) + "ns"); }
C@Around("execution(* com.example.service.*.*(..))") public Object measureTime(ProceedingJoinPoint pjp) throws Throwable { long start = System.currentTimeMillis(); Object result = pjp.proceed(); long end = System.currentTimeMillis(); System.out.println("Execution time: " + (end - start) + "ms"); return result; }
D@Around("execution(* com.example.service.*.*(..))") public Object measureTime(JoinPoint jp) throws Throwable { long start = System.currentTimeMillis(); Object result = jp.proceed(); long end = System.currentTimeMillis(); System.out.println("Execution time: " + (end - start) + "ms"); return result; }
Step-by-Step Solution
Solution:
  1. Step 1: Verify method signature and parameter

    Must accept ProceedingJoinPoint and return Object with throws Throwable.
  2. Step 2: Check timing logic and proceed() call

    Start time before proceed(), end time after, print difference, return result.
  3. Final Answer:

    @Around("execution(* com.example.service.*.*(..))") public Object measureTime(ProceedingJoinPoint pjp) throws Throwable { long start = System.currentTimeMillis(); Object result = pjp.proceed(); long end = System.currentTimeMillis(); System.out.println("Execution time: " + (end - start) + "ms"); return result; } -> Option C
  4. Quick Check:

    Use ProceedingJoinPoint and return result for timing advice [OK]
Quick Trick: Use System.currentTimeMillis() before and after proceed() [OK]
Common Mistakes:
  • Using void return type in @Around advice
  • Missing ProceedingJoinPoint parameter
  • Using JoinPoint instead of ProceedingJoinPoint

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes