0
0
Spring Bootframework~10 mins

@Before advice in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @Before advice
Method call starts
@Before advice runs
Original method executes
Method call ends
When a method is called, the @Before advice runs first, then the original method runs, then the call ends.
Execution Sample
Spring Boot
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class MyAspect {
  @Before("execution(* MyService.doWork(..))")
  public void beforeAdvice() {
    System.out.println("Before advice runs");
  }
}
This code runs the beforeAdvice method before MyService.doWork() executes.
Execution Table
StepActionOutputNext Step
1Call MyService.doWork()No output yetRun @Before advice
2Run @Before advicePrints: Before advice runsRun original method
3Run original method doWork()Original method output (if any)Method call ends
4Method call endsNo further actionExit
💡 Method call completes after original method runs
Variable Tracker
VariableStartAfter Step 2After Step 3Final
methodCallnot startedbeforeAdvice executedoriginal method executedcompleted
Key Moments - 2 Insights
Does the original method run before or after the @Before advice?
The @Before advice runs first, then the original method runs, as shown in execution_table step 2 and 3.
Can @Before advice change the return value of the original method?
No, @Before advice runs before the method but cannot change its return value; it only runs code before the method executes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 2?
AThe original method runs
BThe @Before advice runs
CThe method call ends
DNo action happens
💡 Hint
Check the 'Action' column for step 2 in the execution_table
At which step does the original method execute?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for when the original method runs
If the @Before advice prints a message, when will it appear relative to the original method's output?
AAfter the original method's output
BAt the same time as the original method's output
CBefore the original method's output
DIt will not appear
💡 Hint
Refer to the order of outputs in execution_table steps 2 and 3
Concept Snapshot
@Before advice runs before the target method.
It cannot change the method's return value.
Use @Before("pointcut") to specify where it applies.
It is useful for logging or setup before method runs.
Execution order: @Before advice -> original method -> end.
Full Transcript
When a method matched by the pointcut is called, the @Before advice runs first. This means any code inside the @Before method executes before the original method starts. For example, if you have a method doWork() and a @Before advice on it, the advice prints a message before doWork() runs. The method call then continues to the original method. The @Before advice cannot change the return value or stop the method; it only runs code before the method executes. This is useful for logging or preparing data before the main method runs.