0
0
Spring Bootframework~10 mins

AOP for logging in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a pointcut expression that matches all methods in the service package.

Spring Boot
@Pointcut("[1]")
public void serviceMethods() {}
Drag options to blanks, or click blank then click option'
Awithin(com.example.repository..*)
Bexecution(public void com.example.controller..*(..))
Cexecution(* com.example.service..*(..))
Dbean(*Service)
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong package name in the expression.
Using 'within' instead of 'execution' for method matching.
2fill in blank
medium

Complete the advice method to log before method execution using AOP.

Spring Boot
@Before("serviceMethods()")
public void logBefore(JoinPoint joinPoint) {
    System.out.println("[1]" + joinPoint.getSignature().getName());
}
Drag options to blanks, or click blank then click option'
A"Calling method: "
B"Finished method: "
C"Error in method: "
D"Starting method: "
Attempts:
3 left
💡 Hint
Common Mistakes
Logging after the method instead of before.
Using a message that implies the method finished.
3fill in blank
hard

Fix the error in the after returning advice to log the returned value.

Spring Boot
@AfterReturning(pointcut = "serviceMethods()", returning = "[1]")
public void logAfterReturning(JoinPoint joinPoint, Object [1]) {
    System.out.println("Method " + joinPoint.getSignature().getName() + " returned: " + [1]);
}
Drag options to blanks, or click blank then click option'
Aresult
BreturnValue
Cvalue
Doutput
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names in the annotation and method parameter.
Forgetting to declare the parameter in the method.
4fill in blank
hard

Fill both blanks to create an around advice that logs before and after method execution.

Spring Boot
@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;
}
Drag options to blanks, or click blank then click option'
AStarting method:
Bproceed
Cexecute
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong method name instead of proceed().
Logging after proceed() but forgetting to log before.
5fill in blank
hard

Fill all three blanks to define a logging aspect class with proper annotations and pointcut.

Spring Boot
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() {}

}
Drag options to blanks, or click blank then click option'
AAspect
BComponent
CBefore
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service instead of @Component.
Forgetting to import AspectJ annotations.