Complete the code to define an aspect in Spring Boot.
@Aspect @Component public class LoggingAspect { @Before("[1]") public void logBefore() { System.out.println("Method is about to execute"); } }
The @Before annotation needs a pointcut expression like execution(* com.example.service.*.*(..)) to specify where to apply the advice.
Complete the code to apply advice after a method returns successfully.
@AfterReturning(pointcut = "execution(* com.example.repo.*.*(..))", returning = "result") public void logAfterReturning([1] result) { System.out.println("Method returned: " + result); }
void as parameter type.The advice method parameter must match the type of the returned object or use Object to accept any type.
Fix the error in the advice method signature to correctly capture exceptions.
@AfterThrowing(pointcut = "execution(* com.example.service.*.*(..))", throwing = "ex") public void logAfterThrowing([1] ex) { System.out.println("Exception thrown: " + ex.getMessage()); }
The advice method must accept an Exception or subclass to capture thrown exceptions.
Fill both blanks to create a pointcut that matches all methods in service package and apply advice before execution.
@Aspect @Component public class AuditAspect { @[1]("[2]") public void audit() { System.out.println("Audit: method called"); } }
@After instead of @Before.The @Before annotation applies advice before method execution, and the pointcut expression matches all methods in the service package.
Fill all three blanks to create an around advice that logs method start, proceeds with method execution, and logs method end.
@Around("[1]") public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("Start: " + joinPoint.getSignature().getName()); Object result = joinPoint.[2](); System.out.println("End: " + joinPoint.getSignature().getName()); return [3]; }
The @Around advice uses a pointcut expression to match service methods, calls proceed() to continue execution, and returns the result.