Bird
Raised Fist0
Spring Bootframework~20 mins

@Before advice in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Spring AOP @Before Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a @Before advice is applied in Spring AOP?
Consider a Spring Boot application using AspectJ style @Before advice. What is the behavior of the @Before advice when the target method is called?
Spring Boot
@Aspect
@Component
public class LoggingAspect {
  @Before("execution(* com.example.service.*.*(..))")
  public void logBefore() {
    System.out.println("Method is about to execute");
  }
}
AThe advice runs before the target method executes, printing the message each time the method is called.
BThe advice runs after the target method executes, printing the message only if the method completes successfully.
CThe advice runs only if the target method throws an exception.
DThe advice runs concurrently with the target method, possibly causing race conditions.
Attempts:
2 left
💡 Hint
Think about when @Before advice is triggered in the method call lifecycle.
📝 Syntax
intermediate
2:00remaining
Identify the correct syntax for a @Before advice method in Spring AOP
Which of the following method signatures correctly defines a @Before advice in a Spring Boot Aspect class?
A
@Before
public void beforeMethod() {}
B
@Before("execution(* com.example..*(..))")
public void beforeMethod(String arg) {}
C
@Before("execution(* com.example..*(..))")
public String beforeMethod() { return "ok"; }
D
@Before("execution(* com.example..*(..))")
public void beforeMethod() {}
Attempts:
2 left
💡 Hint
Check the annotation usage and method signature requirements for @Before advice.
🔧 Debug
advanced
2:00remaining
Why does this @Before advice not execute?
Given the following Aspect code, why does the @Before advice never run when calling methods in com.example.service.UserService?
Spring Boot
package com.example.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {

  @Before("execution(* com.example.service.UserService.*(..))")
  public void logBefore() {
    System.out.println("Before method call");
  }
}

// UserService is annotated with @Service and methods are public.
AThe aspect class is missing @EnableAspectJAutoProxy annotation in the Spring configuration.
BThe pointcut expression is incorrect; it should use 'within' instead of 'execution'.
CThe advice method must return a value to execute.
DThe UserService methods are private, so advice cannot be applied.
Attempts:
2 left
💡 Hint
Check if Spring AOP proxying is enabled in the configuration.
❓ state_output
advanced
2:00remaining
What is the output when @Before advice modifies a method argument?
Consider this Aspect and service method. What will be printed when calling process("hello")?
Spring Boot
package com.example.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ModifyArgAspect {
  @Before("execution(* com.example.service.MessageService.process(String)) && args(msg)")
  public void beforeProcess(JoinPoint jp, String msg) {
    System.out.println("Before: " + msg);
    msg = msg.toUpperCase();
  }
}

package com.example.service;

public class MessageService {
  public void process(String message) {
    System.out.println("Processing: " + message);
  }
}
A
Before: hello
Processing: HELLO
B
Before: hello
Processing: hello
C
Before: HELLO
Processing: hello
DCompilation error due to argument modification in advice
Attempts:
2 left
💡 Hint
Think about whether changing the parameter inside advice affects the original method argument.
🧠 Conceptual
expert
2:00remaining
Which statement about @Before advice and exceptions is true?
In Spring AOP, what happens if a @Before advice throws an exception during execution?
AThe exception is caught and logged, then the target method executes.
BThe exception is ignored, and the target method executes normally.
CThe target method is not executed, and the exception propagates to the caller.
DThe target method executes first, then the exception is thrown.
Attempts:
2 left
💡 Hint
Consider the order of execution and exception handling in advice.

Practice

(1/5)
1. What is the main purpose of the @Before advice in Spring AOP?
easy
A. To replace the matched method entirely
B. To run code right before the matched method executes
C. To run code after the matched method completes
D. To handle exceptions thrown by the matched method

Solution

  1. Step 1: Understand the role of @Before advice

    @Before advice is designed to execute code just before the target method runs.
  2. Step 2: Compare with other advice types

    @After runs after method completion, @Around can replace, and @AfterThrowing handles exceptions.
  3. Final Answer:

    To run code right before the matched method executes -> Option B
  4. Quick Check:

    @Before advice = runs before method [OK]
Hint: Remember: @Before means before method starts [OK]
Common Mistakes:
  • Confusing @Before with @After or @Around advice
  • Thinking @Before replaces the method
  • Assuming @Before handles exceptions
2. Which of the following is the correct syntax to define a @Before advice method in Spring AOP?
easy
A. @Before("execution(* com.example.service.*.*(..))") public void logBefore() {}
B. @Before public void logBefore() { execution(* com.example.service.*.*(..)); }
C. @Before public void logBefore() execution(* com.example.service.*.*(..)) {}
D. @Before("logBefore()") public void execution(* com.example.service.*.*(..)) {}

Solution

  1. Step 1: Identify correct @Before annotation usage

    The @Before annotation requires a pointcut expression as a string parameter inside parentheses and quotes.
  2. Step 2: Check method signature and placement

    The method should be public void with no parameters or with JoinPoint if needed, and the pointcut is inside @Before("...").
  3. Final Answer:

    @Before("execution(* com.example.service.*.*(..))") public void logBefore() {} -> Option A
  4. Quick Check:

    @Before("pointcut") public void method() {} [OK]
Hint: Look for @Before with pointcut string in parentheses [OK]
Common Mistakes:
  • Placing pointcut expression outside quotes
  • Missing parentheses after @Before
  • Incorrect method signature or missing public
3. Given the following code snippet, what will be printed when service.process() is called?
@Aspect
@Component
public class LoggingAspect {
  @Before("execution(* com.example.service.Service.process(..))")
  public void beforeProcess() {
    System.out.println("Before process");
  }
}

@Service
public class Service {
  public void process() {
    System.out.println("Processing");
  }
}
medium
A. Before process\nProcessing
B. Processing\nBefore process
C. Before process
D. Processing

Solution

  1. Step 1: Understand @Before advice execution timing

    @Before advice runs before the matched method, so "Before process" prints first.
  2. Step 2: Analyze method call output

    Then the actual method prints "Processing" after the advice.
  3. Final Answer:

    Before process\nProcessing -> Option A
  4. Quick Check:

    @Before prints first, then method output [OK]
Hint: @Before prints before method output [OK]
Common Mistakes:
  • Assuming method prints before advice
  • Thinking advice replaces method output
  • Ignoring advice execution order
4. Identify the error in this @Before advice definition:
@Before("execution(* com.example..*(..))")
public void beforeMethod() {
  System.out.println("Before method");
}
medium
A. Advice method cannot have a body
B. Pointcut expression syntax is invalid
C. Method must return a value
D. Missing @Aspect annotation on the class

Solution

  1. Step 1: Check class-level annotations

    For @Before advice to work, the class must be annotated with @Aspect to mark it as an aspect.
  2. Step 2: Validate pointcut and method

    The pointcut expression is valid, and advice methods can have bodies and return void.
  3. Final Answer:

    Missing @Aspect annotation on the class -> Option D
  4. Quick Check:

    @Aspect missing = advice won't run [OK]
Hint: Always add @Aspect on the class with advice [OK]
Common Mistakes:
  • Forgetting @Aspect on the aspect class
  • Assuming advice methods must return values
  • Misreading pointcut syntax errors
5. You want to log a message before any method in com.example.repository package is called, but only for methods starting with save. Which @Before pointcut expression correctly matches this?
hard
A. @Before("execution(* com.example.repository.save*(..))")
B. @Before("execution(* com.example.repository.*save*(..))")
C. @Before("execution(* com.example.repository..save*(..))")
D. @Before("execution(* com.example.repository.*.save*(..))")

Solution

  1. Step 1: Understand pointcut syntax for package, class, and method

    To match methods starting with 'save' in any class of the com.example.repository package, use execution(* com.example.repository.*.save*(..)). However, if you want to include subpackages as well, use '..' instead of a single '*'.
  2. Step 2: Analyze each option

    A matches only methods named save* in the package directly (no class wildcard), B is invalid syntax, C matches methods in any class in the package but not subpackages, D matches methods starting with save in the package and all subpackages.
  3. Final Answer:

    @Before("execution(* com.example.repository..save*(..))") -> Option C
  4. Quick Check:

    package..save*(..) = matches package and subpackages [OK]
Hint: Use package..save* to include subpackages and methods starting with save [OK]
Common Mistakes:
  • Using incorrect wildcards or dots in method name
  • Matching subpackages unintentionally
  • Misplacing wildcards in pointcut expression
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