Bird
Raised Fist0
Spring Bootframework~10 mins

AOP for logging in Spring Boot - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - AOP for logging
Start Application
↓
Method Call
↓
AOP Interceptor
↓
Log Before
↓
Log After
↓
Return Result
↓
End Method Execution
When a method is called, AOP intercepts it to log before and after execution, then proceeds with the method and returns the result.
Execution Sample
Spring Boot
@Aspect
@Component
public class LoggingAspect {
  @Before("execution(* com.example..*(..))")
  public void logBefore() { System.out.println("Method start"); }
  @After("execution(* com.example..*(..))")
  public void logAfter() { System.out.println("Method end"); }
}
This code logs messages before and after any method in com.example package runs.
Execution Table
StepActionEvaluationResult
1Application startsSpring loads beansLoggingAspect bean created
2Call method com.example.Service.doWork()Matches pointcut expressionAOP interceptor triggers
3Execute @Before advicePrint 'Method start'Message logged before method
4Proceed with original methodMethod executes normallyMethod logic runs
5Execute @After advicePrint 'Method end'Message logged after method
6Return method resultMethod returns valueCaller receives result
💡 Method execution completes, AOP advices finished
Variable Tracker
VariableStartAfter Step 3After Step 5Final
logMessages[]["Method start"]["Method start", "Method end"]["Method start", "Method end"]
methodResultnullnullnullsomeValue
Key Moments - 3 Insights
Why does the logBefore method run before the actual method?
Because the @Before advice is triggered by the pointcut before the method executes, as shown in execution_table step 3.
Does the original method run before or after the logging?
The original method runs after the @Before advice and before the @After advice, as shown in execution_table step 4.
What happens if the method throws an exception?
The @After advice still runs after method execution, but to log only on success or failure, use @AfterReturning or @AfterThrowing advices.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is logged at step 3?
ANo log
B"Method start"
C"Method end"
D"Method running"
💡 Hint
Check the 'Action' and 'Result' columns at step 3 in the execution_table.
At which step does the original method execute?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for 'Proceed with original method' in the 'Action' column.
If we remove the @After advice, what changes in the variable 'logMessages'?
AIt will contain both "Method start" and "Method end"
BIt will be empty
CIt will only contain "Method start"
DIt will contain only "Method end"
💡 Hint
Refer to variable_tracker row for 'logMessages' after Step 5.
Concept Snapshot
AOP for logging in Spring Boot:
- Use @Aspect and @Component to define aspect class
- Use @Before to log before method execution
- Use @After to log after method execution
- Pointcut expression defines which methods to intercept
- Logs help track method start and end without changing method code
Full Transcript
This visual execution shows how Spring Boot uses AOP for logging. When the application starts, the LoggingAspect bean is created. When a method in the com.example package is called, the AOP interceptor triggers. First, the @Before advice logs 'Method start'. Then the original method runs. After it finishes, the @After advice logs 'Method end'. Finally, the method returns its result. Variables track the logged messages and method result. Key moments clarify why logging happens before and after the method. The quiz tests understanding of the execution steps and variable changes.

Practice

(1/5)
1. What is the main purpose of using AOP (Aspect-Oriented Programming) for logging in Spring Boot?
easy
A. To write logging code inside every method manually
B. To replace all existing logging frameworks
C. To add logging without changing the main business code
D. To disable logging for all methods automatically

Solution

  1. Step 1: Understand AOP's role in logging

    AOP allows adding extra behavior like logging without modifying the main code logic.
  2. Step 2: Identify the benefit of separation

    By using AOP, logging code is kept separate, making the main code cleaner and easier to maintain.
  3. Final Answer:

    To add logging without changing the main business code -> Option C
  4. Quick Check:

    AOP adds logging cleanly = A [OK]
Hint: AOP separates logging from business code [OK]
Common Mistakes:
  • Thinking AOP replaces logging frameworks
  • Believing logging must be written inside each method
  • Assuming AOP disables logging automatically
2. Which annotation is used in Spring AOP to execute code before a method runs for logging purposes?
easy
A. @AfterReturning
B. @AfterThrowing
C. @Around
D. @Before

Solution

  1. Step 1: Recall Spring AOP annotations

    @Before runs advice before the target method executes, perfect for logging method start.
  2. Step 2: Differentiate from other annotations

    @AfterReturning runs after method success, @Around wraps method, @AfterThrowing runs on exceptions.
  3. Final Answer:

    @Before -> Option D
  4. Quick Check:

    Log before method = @Before [OK]
Hint: Use @Before to log method start [OK]
Common Mistakes:
  • Confusing @Before with @AfterReturning
  • Using @AfterThrowing for normal logging
  • Thinking @Around is only for exceptions
3. Given this Spring AOP advice snippet, what will be logged when processOrder() method completes successfully?
@Before("execution(* com.example.service.*.*(..))")
public void logStart() {
    System.out.println("Method started");
}

@AfterReturning("execution(* com.example.service.*.*(..))")
public void logEnd() {
    System.out.println("Method completed");
}
medium
A. Method started and Method completed
B. Method completed only
C. Method started only
D. No output logged

Solution

  1. Step 1: Analyze @Before advice

    Before processOrder() runs, "Method started" is printed.
  2. Step 2: Analyze @AfterReturning advice

    After successful completion, "Method completed" is printed.
  3. Final Answer:

    Method started and Method completed -> Option A
  4. Quick Check:

    Before + AfterReturning logs both messages [OK]
Hint: Both @Before and @AfterReturning run on success [OK]
Common Mistakes:
  • Thinking only one advice runs
  • Confusing @AfterReturning with @AfterThrowing
  • Assuming no logs if method returns void
4. Identify the error in this Spring AOP logging advice code:
@Before("execution(* com.example..*(..))")
public void logStart() {
    System.out.println("Starting method");
}

@AfterReturning("execution(* com.example.service.*.*(..))")
public void logEnd() {
    System.out.println("Method ended");
}
medium
A. The pointcut expressions target different packages
B. The method signatures lack return types
C. The @AfterReturning advice is missing a returning attribute
D. The pointcut expression in @Before is invalid

Solution

  1. Step 1: Check pointcut expressions

    @Before targets "com.example..*" (all subpackages), @AfterReturning targets only "com.example.service.*".
  2. Step 2: Understand mismatch impact

    This mismatch means @Before runs on more methods than @AfterReturning, causing inconsistent logging.
  3. Final Answer:

    The pointcut expressions target different packages -> Option A
  4. Quick Check:

    Pointcut mismatch = A [OK]
Hint: Match pointcut packages exactly for consistent logging [OK]
Common Mistakes:
  • Assuming pointcut syntax is wrong
  • Thinking return type is required in advice methods
  • Believing @AfterReturning must have returning attribute always
5. You want to log method entry, exit, and exceptions for all service methods in package com.example.service. Which combination of Spring AOP advice annotations should you use?
hard
A. @AfterThrowing only
B. @Before, @AfterReturning, and @AfterThrowing
C. @Around only
D. @Before and @AfterReturning only

Solution

  1. Step 1: Identify logging needs

    Entry logging requires @Before, exit logging requires @AfterReturning, exception logging requires @AfterThrowing.
  2. Step 2: Confirm combined advice usage

    Using all three annotations covers all cases: start, successful end, and exceptions.
  3. Final Answer:

    @Before, @AfterReturning, and @AfterThrowing -> Option B
  4. Quick Check:

    All logging cases need these three advices [OK]
Hint: Use all three advices for full logging coverage [OK]
Common Mistakes:
  • Using only @Before and @AfterReturning misses exceptions
  • Thinking @Around alone is simpler for all cases
  • Ignoring exception logging 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