0
0
Spring Bootframework~10 mins

Why AOP matters in Spring Boot - Test Your Understanding

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

Complete the code to define an aspect in Spring Boot.

Spring Boot
@Aspect
@Component
public class LoggingAspect {
    @Before("[1]")
    public void logBefore() {
        System.out.println("Method is about to execute");
    }
}
Drag options to blanks, or click blank then click option'
A"execution(* com.example.service.*.*(..))"
B"@Aspect"
C"@Component"
D"@Before"
Attempts:
3 left
💡 Hint
Common Mistakes
Putting annotation names instead of pointcut expressions.
Leaving the blank empty.
Using incorrect pointcut syntax.
2fill in blank
medium

Complete the code to apply advice after a method returns successfully.

Spring Boot
@AfterReturning(pointcut = "execution(* com.example.repo.*.*(..))", returning = "result")
public void logAfterReturning([1] result) {
    System.out.println("Method returned: " + result);
}
Drag options to blanks, or click blank then click option'
AObject
Bvoid
CString
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using void as parameter type.
Using a specific type that may not match the returned value.
3fill in blank
hard

Fix the error in the advice method signature to correctly capture exceptions.

Spring Boot
@AfterThrowing(pointcut = "execution(* com.example.service.*.*(..))", throwing = "ex")
public void logAfterThrowing([1] ex) {
    System.out.println("Exception thrown: " + ex.getMessage());
}
Drag options to blanks, or click blank then click option'
AString
Bvoid
CException
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-exception types as parameter.
Omitting the parameter.
4fill in blank
hard

Fill both blanks to create a pointcut that matches all methods in service package and apply advice before execution.

Spring Boot
@Aspect
@Component
public class AuditAspect {
    @[1]("[2]")
    public void audit() {
        System.out.println("Audit: method called");
    }
}
Drag options to blanks, or click blank then click option'
ABefore
BAfter
Cexecution(* com.example.service.*.*(..))
Dexecution(* com.example.repo.*.*(..))
Attempts:
3 left
💡 Hint
Common Mistakes
Using @After instead of @Before.
Using pointcut for repo package instead of service.
5fill in blank
hard

Fill all three blanks to create an around advice that logs method start, proceeds with method execution, and logs method end.

Spring Boot
@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];
}
Drag options to blanks, or click blank then click option'
Aexecution(* com.example.controller.*.*(..))
Bproceed
Cresult
Dexecution(* com.example.service.*.*(..))
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong pointcut expression.
Calling a wrong method instead of proceed().
Returning something other than the result.