0
0
Spring Bootframework~10 mins

Cross-cutting concerns concept 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 Spring Boot aspect for logging.

Spring Boot
import org.aspectj.lang.annotation.[1];

@Aspect
public class LoggingAspect {
    // aspect code
}
Drag options to blanks, or click blank then click option'
AComponent
BAspect
CService
DRepository
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @Aspect
Forgetting to import the correct annotation
2fill in blank
medium

Complete the code to define a pointcut expression for all methods in 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*
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' which results in ..*** a non-standard pattern
Using '+' which is used for subclass matching in parameters
3fill in blank
hard

Fix the error in the advice annotation to run before methods matched by pointcut.

Spring Boot
@[1]("serviceMethods()")
public void beforeAdvice() {
    System.out.println("Before method execution");
}
Drag options to blanks, or click blank then click option'
ABefore
BAfter
CAround
DPointcut
Attempts:
3 left
💡 Hint
Common Mistakes
Using @After instead of @Before
Using @Pointcut instead of advice annotation
4fill in blank
hard

Fill both blanks to create an aspect that logs method execution time.

Spring Boot
public @[1] class PerformanceAspect {

    @Around("execution(* com.example..*.*(..))")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object proceed = joinPoint.[2]();
        long executionTime = System.currentTimeMillis() - start;
        System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
        return proceed;
    }
}
Drag options to blanks, or click blank then click option'
AAspect
BComponent
Cproceed
Dproceed()
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @Aspect
Calling proceed without parentheses
5fill in blank
hard

Fill all three blanks to define a Spring bean for an aspect with logging advice.

Spring Boot
@[1]
public class LoggingAspect {

    @[2]("execution(* com.example..*.*(..))")
    public void logBefore() {
        System.out.println("Method called");
    }

    @Bean
    public LoggingAspect [3]() {
        return new LoggingAspect();
    }
}
Drag options to blanks, or click blank then click option'
AAspect
BBefore
CloggingAspect
DComponent
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @Aspect
Using @After instead of @Before
Naming the bean method with uppercase letters