0
0
Spring Bootframework~20 mins

@Aspect annotation in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Aspect Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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.
AExecution time: 50ms
BNo output printed
CExecution time: 0ms
DCompilation error due to missing @Timed annotation definition
Attempts:
2 left
💡 Hint
Think about what the @Around advice does and when the print statement runs.
📝 Syntax
intermediate
2: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?
A@Pointcut("within(com.example.service.*)")
B@Pointcut("execution(* com.example.service.*(..))")
C@Pointcut("execution(* com.example.service..*(..))")
D@Pointcut("execution(* com.example.service.*.*(..))")
Attempts:
2 left
💡 Hint
Remember '..' means recursive subpackages and '*' means any class or method name.
lifecycle
advanced
2: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?
ADuring Spring context startup, alongside other singleton beans before application runs
BAfter all singleton beans are created and the context is fully initialized
CDuring Spring context startup, before any other beans are created
DOnly when the first advised method is called at runtime
Attempts:
2 left
💡 Hint
Think about when Spring creates singleton beans and how proxies are applied.
🔧 Debug
advanced
2: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 } }
AThe @MyAdvice annotation is not recognized by Spring AOP
BThe aspect is not registered as a Spring bean
CmethodB is private and cannot be proxied
DSpring AOP proxies only intercept external method calls, not self-invocations
Attempts:
2 left
💡 Hint
Think about how Spring AOP creates proxies and what calls they intercept.
🧠 Conceptual
expert
2: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?
AThe aspect with @Order(2) runs before the one with @Order(1)
BThe aspect with @Order(1) runs before the one with @Order(2)
COrder does not affect execution sequence of aspects
DBoth aspects run simultaneously in no guaranteed order
Attempts:
2 left
💡 Hint
Lower @Order values have higher precedence in Spring AOP.