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;
}
