Complete the code to define a pointcut expression that matches all methods in the service package.
@Pointcut("[1]") public void serviceMethods() {}
The pointcut expression execution(* com.example.service..*(..)) matches all methods in the service package and its subpackages.
Complete the advice method to log before method execution using AOP.
@Before("serviceMethods()") public void logBefore(JoinPoint joinPoint) { System.out.println("[1]" + joinPoint.getSignature().getName()); }
The @Before advice runs before the method. Logging "Starting method: " shows the method is about to run.
Fix the error in the after returning advice to log the returned value.
@AfterReturning(pointcut = "serviceMethods()", returning = "[1]") public void logAfterReturning(JoinPoint joinPoint, Object [1]) { System.out.println("Method " + joinPoint.getSignature().getName() + " returned: " + [1]); }
The name used in the returning attribute must match the parameter name in the advice method. Here, result is used consistently.
Fill both blanks to create an around advice that logs before and after method execution.
@Around("serviceMethods()") public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("[1]" + joinPoint.getSignature().getName()); Object result = joinPoint.[2](); System.out.println("Finished method: " + joinPoint.getSignature().getName()); return result; }
The around advice uses proceed() to continue method execution. Logging before and after helps trace the method call.
Fill all three blanks to define a logging aspect class with proper annotations and pointcut.
import org.aspectj.lang.annotation.[1]; import org.aspectj.lang.annotation.[2]; import org.springframework.stereotype.[3]; @Aspect @[3] public class LoggingAspect { @Pointcut("execution(* com.example.service..*(..))") public void serviceMethods() {} }
The class must import Aspect and Before annotations from AspectJ and be annotated with @Component to be recognized by Spring.