Complete the code to define an aspect class for performance monitoring.
import org.aspectj.lang.annotation.[1]; @Aspect public class PerformanceAspect { }
The @Aspect annotation marks the class as an aspect for AOP.
Complete the code to define a pointcut expression that matches all methods in the service package.
@Pointcut("execution(* com.example.service..*[1]*(..))") public void serviceMethods() {}
The * wildcard matches any method name in the specified package.
Fix the error in the advice method to correctly measure execution time.
@Around("serviceMethods()") public Object measureTime(ProceedingJoinPoint joinPoint) throws Throwable { long start = System.currentTimeMillis(); Object result = joinPoint.[1](); long end = System.currentTimeMillis(); System.out.println("Execution time: " + (end - start) + " ms"); return result; }
The proceed() method executes the target method and returns its result.
Fill both blanks to log the method name and execution time inside the advice.
String methodName = joinPoint.getSignature().[1](); System.out.println("Method " + methodName + " took " + [2] + " ms");
getName() gets the method name. The difference end - start gives execution time.
Fill all three blanks to create a complete around advice that measures and logs execution time for service methods.
@Around("[1]") public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long [2] = System.currentTimeMillis(); Object result = joinPoint.proceed(); long [3] = System.currentTimeMillis(); System.out.println("Execution time: " + (end - start) + " ms"); return result; }
The pointcut serviceMethods() targets service methods. Variables start and end store timestamps before and after method execution.