Challenge - 5 Problems
Aspect Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when an @Aspect logs method execution time?
Consider an @Aspect that logs the execution time of a method annotated with @Timed. What will be printed when the method runs?
Spring Boot
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect @Component public class TimingAspect { @Around("@annotation(Timed)") public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long start = System.currentTimeMillis(); Object proceed = joinPoint.proceed(); long duration = System.currentTimeMillis() - start; System.out.println("Execution time: " + duration + "ms"); return proceed; } } // Assume a method annotated with @Timed is called and takes 50ms to execute.
Attempts:
2 left
💡 Hint
Think about what the @Around advice does and when the print statement runs.
✗ Incorrect
The @Around advice wraps the method call, measuring time before and after execution. It prints the actual duration, so if the method takes 50ms, it prints 'Execution time: 50ms'.
📝 Syntax
intermediate2:00remaining
Which @Aspect syntax correctly matches all methods in a package?
You want an @Aspect to intercept all methods in the package com.example.service. Which pointcut expression is correct?
Attempts:
2 left
💡 Hint
Remember '..' means recursive subpackages and '*' means any class or method name.
✗ Incorrect
The expression 'execution(* com.example.service..*(..))' matches any method in com.example.service and its subpackages. Option C matches only methods in classes directly in com.example.service package, not subpackages. Option C ('within(com.example.service.*)') matches method executions in classes directly in the package using within, not subpackages. Option C is incorrect because the declaring type pattern 'com.example.service.*.*' is invalid syntax.
❓ lifecycle
advanced2:00remaining
When is an @Aspect bean initialized in Spring Boot lifecycle?
At what point in the Spring Boot application lifecycle is an @Aspect bean created and ready to intercept method calls?
Attempts:
2 left
💡 Hint
Think about when Spring creates singleton beans and how proxies are applied.
✗ Incorrect
Spring creates @Aspect beans as singleton beans during context startup, alongside other singleton beans. The proxies for advised beans are created then, so aspects are ready before application logic runs.
🔧 Debug
advanced2:00remaining
Why does this @Aspect advice not run for a self-invoked method?
Given this code, why does the @Aspect advice not trigger when methodA calls methodB inside the same class?
public class MyService {
@MyAdvice
public void methodA() {
methodB();
}
@MyAdvice
public void methodB() {
// do something
}
}
Attempts:
2 left
💡 Hint
Think about how Spring AOP creates proxies and what calls they intercept.
✗ Incorrect
Spring AOP uses proxies that intercept calls from outside the bean. Internal calls like methodA calling methodB directly do not go through the proxy, so advice on methodB is not triggered.
🧠 Conceptual
expert2:00remaining
What is the effect of @Aspect precedence with @Order annotation?
You have two @Aspect classes with @Order(1) and @Order(2). Which advice runs first when both apply to the same method?
Attempts:
2 left
💡 Hint
Lower @Order values have higher precedence in Spring AOP.
✗ Incorrect
In Spring AOP, aspects with lower @Order values have higher precedence and their advice runs first. So @Order(1) runs before @Order(2).