Bird
Raised Fist0
Spring Bootframework~10 mins

@Aspect annotation in Spring Boot - Interactive Code Practice

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

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