Bird
Raised Fist0
Spring Bootframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the primary purpose of the @Aspect annotation in Spring Boot?
easy
A. To schedule tasks to run at fixed intervals
B. To define a REST controller for handling HTTP requests
C. To configure database connections automatically
D. To mark a class as an aspect that can add extra behavior to other parts of the program

Solution

  1. Step 1: Understand the role of @Aspect

    The @Aspect annotation marks a class as an aspect, which means it can add extra behavior to other parts of the program without changing their code.
  2. Step 2: Differentiate from other annotations

    Other options like REST controllers, database configuration, or scheduling are unrelated to @Aspect.
  3. Final Answer:

    To mark a class as an aspect that can add extra behavior to other parts of the program -> Option D
  4. Quick Check:

    @Aspect = marks aspect class [OK]
Hint: Remember: @Aspect marks classes for extra behavior [OK]
Common Mistakes:
  • Confusing @Aspect with @Controller
  • Thinking @Aspect manages database or scheduling
  • Assuming @Aspect runs HTTP endpoints
2. Which of the following is the correct way to declare an aspect class in Spring Boot?
easy
A. @Aspect public class MyAspect {}
B. public class MyAspect @Aspect {}
C. @Aspect() void MyAspect() {}
D. class MyAspect @Aspect public {}

Solution

  1. Step 1: Check correct annotation placement

    The @Aspect annotation must be placed before the class declaration, like @Aspect public class MyAspect {}.
  2. Step 2: Identify invalid syntax

    Options A, B, and D place @Aspect incorrectly or misuse syntax, causing errors.
  3. Final Answer:

    @Aspect public class MyAspect {} -> Option A
  4. Quick Check:

    @Aspect before class name = correct syntax [OK]
Hint: Put @Aspect right before the class keyword [OK]
Common Mistakes:
  • Placing @Aspect after class name
  • Using @Aspect on methods instead of classes
  • Incorrect parentheses or missing class keyword
3. Given this aspect code snippet, what will be printed when someMethod() is called?
@Aspect
public class LoggingAspect {
  @Before("execution(* someMethod())")
  public void logBefore() {
    System.out.println("Before method");
  }
}
medium
A. After method
B. No output
C. Before method
D. Compilation error

Solution

  1. Step 1: Understand @Before advice

    The @Before annotation runs the logBefore() method before someMethod() executes, printing "Before method".
  2. Step 2: Confirm no errors or other outputs

    There is no @After advice or errors, so only "Before method" prints.
  3. Final Answer:

    Before method -> Option C
  4. Quick Check:

    @Before runs before target method [OK]
Hint: @Before prints before method runs [OK]
Common Mistakes:
  • Confusing @Before with @After
  • Expecting no output if unaware of advice
  • Assuming syntax errors without checking code
4. Identify the error in this aspect code:
@Aspect
public class MyAspect {
  @Before("execution(* someMethod())")
  public void logBefore() {
    System.out.println("Before method");
  }

  public void someMethod() {
    System.out.println("Inside someMethod");
  }
}
medium
A. The pointcut expression is incorrect
B. The aspect class must be annotated with @Component or registered as a bean
C. The advice method logBefore() must return a value
D. The someMethod() cannot be inside the aspect class

Solution

  1. Step 1: Check aspect registration

    In Spring Boot, an aspect class must be a Spring bean to work. It needs @Component or manual bean registration.
  2. Step 2: Validate other code parts

    The pointcut expression and advice method are correct. Having someMethod() inside the aspect is allowed but unusual.
  3. Final Answer:

    The aspect class must be annotated with @Component or registered as a bean -> Option B
  4. Quick Check:

    Aspect needs bean registration [OK]
Hint: Aspect classes must be Spring beans to work [OK]
Common Mistakes:
  • Ignoring @Component or bean registration
  • Thinking advice methods must return values
  • Assuming pointcut syntax is wrong without checking
5. You want to log execution time of all methods in a service class using @Aspect. Which approach correctly applies this?
hard
A. Use @Around advice with a pointcut matching all methods in the service class and measure time before and after proceeding
B. Use @Before advice to start timer and @After advice to stop timer separately
C. Add logging code directly inside each service method instead of using @Aspect
D. Use @AfterThrowing advice to log execution time only when exceptions occur

Solution

  1. Step 1: Understand timing with @Around advice

    @Around advice can run code before and after the target method, allowing precise measurement of execution time.
  2. Step 2: Evaluate other options

    @Before and @After separately cannot measure total time easily; direct logging mixes concerns; @AfterThrowing only runs on exceptions, missing normal cases.
  3. Final Answer:

    Use @Around advice with a pointcut matching all methods in the service class and measure time before and after proceeding -> Option A
  4. Quick Check:

    @Around advice best for timing [OK]
Hint: Use @Around to wrap method and measure time [OK]
Common Mistakes:
  • Using separate @Before and @After for timing
  • Mixing logging code inside business methods
  • Logging only on exceptions with @AfterThrowing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard