0
0
Spring Bootframework~10 mins

@Aspect annotation in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @Aspect annotation
Define Aspect Class
Add @Aspect Annotation
Define Pointcut Expression
Define Advice Method (e.g., @Before)
Spring AOP Proxy Created
Target Method Called
Advice Runs Before/After Target Method
Target Method Executes
Return to Caller
This flow shows how an @Aspect class is defined, advice methods are set, and how Spring AOP runs advice around target method calls.
Execution Sample
Spring Boot
import org.aspectj.lang.annotation.*;

@Aspect
public class LoggingAspect {
  @Before("execution(* com.example.service.*.*(..))")
  public void logBefore() {
    System.out.println("Method is about to run");
  }
}
Defines an aspect with a @Before advice that logs before any method in com.example.service package runs.
Execution Table
StepActionEvaluationResult
1Spring scans for @Aspect classesFinds LoggingAspectLoggingAspect registered as aspect
2Spring creates proxy for target beansTarget beans in com.example.serviceProxies wrap target beans
3Client calls target method serviceMethod()Matches pointcut expressionAdvice logBefore() triggered
4Advice logBefore() runsPrints messageOutput: 'Method is about to run'
5Target method serviceMethod() runsExecutes original logicTarget method completes
6Return to clientMethod call endsExecution finished
💡 Execution stops after target method completes and returns to caller
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
LoggingAspect instancenullcreatedactiveactiveactive
Target method callnoneinvokedwaitingexecutingcompleted
Advice triggeredfalsetruetruefalsefalse
Key Moments - 3 Insights
Why does the advice run before the target method?
Because the @Before annotation tells Spring AOP to run the advice method before the matched target method, as shown in execution_table step 4.
How does Spring know which methods to apply the advice to?
Spring uses the pointcut expression in the @Before annotation to match method signatures, as seen in execution_table step 3.
Is the original target method replaced by the advice?
No, the advice runs around the target method but does not replace it; the target method still executes after advice, as shown in steps 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 4?
AThe target method starts executing
BSpring scans for @Aspect classes
CThe advice method logBefore() runs and prints a message
DThe client receives the method result
💡 Hint
Check the 'Action' and 'Result' columns at step 4 in execution_table
At which step does the target method actually execute?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look for 'Target method runs' in the 'Action' column of execution_table
If the pointcut expression did not match any method, what would change in the execution_table?
AAdvice would still run before the target method
BAdvice would never run, target method runs normally
CSpring would not create any proxies
DThe target method would be replaced by advice
💡 Hint
Refer to step 3 where matching triggers advice; no match means no advice
Concept Snapshot
@Aspect annotation marks a class as an aspect.
Use pointcut expressions to select methods.
Advice methods (@Before, @After, etc.) run around target methods.
Spring creates proxies to apply advice.
Advice runs without replacing original methods.
Useful for logging, security, transactions.
Full Transcript
The @Aspect annotation in Spring Boot marks a class as an aspect that contains advice methods. Spring scans for these classes and creates proxies for target beans matching pointcut expressions. When a client calls a target method, Spring runs the advice method first if annotated with @Before, then executes the target method. This allows adding behavior like logging without changing the original code. The execution flow starts with scanning, proxy creation, method call, advice execution, target method execution, and finally returning to the caller. Variables like the aspect instance and advice trigger state change during execution. Common confusions include why advice runs before the method, how Spring matches methods, and that advice does not replace the target method. Visual quizzes reinforce understanding of each step.