Complete the code to define a Spring Boot aspect for logging.
import org.aspectj.lang.annotation.[1]; @Aspect public class LoggingAspect { // aspect code }
The @Aspect annotation marks the class as an aspect for cross-cutting concerns like logging.
Complete the code to define a pointcut expression for all methods in service package.
@Pointcut("execution(* com.example.service..*[1]*(..))") public void serviceMethods() {}
The . separates the class name pattern * from the method name pattern * in the execution pointcut.
Fix the error in the advice annotation to run before methods matched by pointcut.
@[1]("serviceMethods()") public void beforeAdvice() { System.out.println("Before method execution"); }
The @Before annotation runs advice before the matched methods.
Fill both blanks to create an aspect that logs method execution time.
public @[1] class PerformanceAspect { @Around("execution(* com.example..*.*(..))") public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long start = System.currentTimeMillis(); Object proceed = joinPoint.[2](); long executionTime = System.currentTimeMillis() - start; System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms"); return proceed; } }
The class must be annotated with @Aspect to define an aspect. The method proceed() runs the original method.
Fill all three blanks to define a Spring bean for an aspect with logging advice.
@[1] public class LoggingAspect { @[2]("execution(* com.example..*.*(..))") public void logBefore() { System.out.println("Method called"); } @Bean public LoggingAspect [3]() { return new LoggingAspect(); } }
The class must be annotated with @Aspect to define an aspect. The advice uses @Before. The bean method name is usually camelCase.