0
0
Spring Bootframework~10 mins

@Aspect annotation 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 declare a class as an aspect using the correct annotation.

Spring Boot
@[1]
public class LoggingAspect {
    // aspect code here
}
Drag options to blanks, or click blank then click option'
AAspect
BComponent
CService
DRepository
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @Aspect
Forgetting the '@' symbol
Using unrelated annotations like @Service
2fill in blank
medium

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

Spring Boot
@Pointcut("execution(* com.example.service..[1]*(..))")
public void serviceMethods() {}
Drag options to blanks, or click blank then click option'
A#
B.
C+
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.' which matches package separator
Using '+' or '#' which are not valid here
3fill in blank
hard

Fix the error in the advice method declaration by completing the annotation.

Spring Boot
@[1]("serviceMethods()")
public void logBefore() {
    System.out.println("Method is about to execute");
}
Drag options to blanks, or click blank then click option'
ABefore
BAfterReturning
CAround
DAfter
Attempts:
3 left
💡 Hint
Common Mistakes
Using @After instead of @Before
Using @Around without proper method signature
4fill in blank
hard

Fill both blanks to create an aspect class that logs before and after method execution.

Spring Boot
@Aspect
public class LoggingAspect {
    @[1]("execution(* com.example..*(..))")
    public void logStart() {
        System.out.println("Start method");
    }

    @[2]("execution(* com.example..*(..))")
    public void logEnd() {
        System.out.println("End method");
    }
}
Drag options to blanks, or click blank then click option'
ABefore
BAfterReturning
CAfter
DAround
Attempts:
3 left
💡 Hint
Common Mistakes
Using @AfterReturning instead of @After for the second advice
Mixing up the order of annotations
5fill in blank
hard

Fill all three blanks to define an around advice that logs method execution time.

Spring Boot
@Aspect
public class PerformanceAspect {
    @Around("[1]")
    public Object measureTime(ProceedingJoinPoint [2]) throws Throwable {
        long start = System.currentTimeMillis();
        Object result = [3].proceed();
        long end = System.currentTimeMillis();
        System.out.println("Execution time: " + (end - start) + "ms");
        return result;
    }
}
Drag options to blanks, or click blank then click option'
Aexecution(* com.example..*(..))
BjoinPoint
Dexecution(* com.example.service..*(..))
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong pointcut expression
Mismatching parameter name and usage
Forgetting to call proceed()