0
0
Spring Bootframework~10 mins

@After and @AfterReturning in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @After and @AfterReturning
Method Execution Start
Method Runs Normally
@AfterReturning
Method Execution End
Shows how @After runs always after a method finishes, while @AfterReturning runs only if the method completes without exception.
Execution Sample
Spring Boot
@After("execution(* com.example.service.*.*(..))")
public void afterAdvice() {
    System.out.println("After advice runs always");
}

@AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result")
public void afterReturningAdvice(Object result) {
    System.out.println("AfterReturning advice runs with result: " + result);
}
Defines two advices: one runs always after methods in service package, the other runs only if method returns normally.
Execution Table
StepMethod Outcome@After Triggered@AfterReturning TriggeredOutput
1Method startsNoNoMethod execution begins
2Method runs normallyNoNoMethod logic executes
3Method returns valueNoNoMethod returns 'Success'
4After method finishesYesYesPrint: After advice runs always
5After method finishesYesYesPrint: AfterReturning advice runs with result: Success
6Method startsNoNoMethod execution begins
7Method throws exceptionNoNoException thrown
8After method finishesYesNoPrint: After advice runs always
9EndNoNoNo AfterReturning because of exception
💡 Execution stops after method finishes; @After always runs, @AfterReturning runs only if no exception.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7After Step 8Final
Method OutcomeNot startedReturns 'Success'Returns 'Success'Throws ExceptionThrows ExceptionFinished
@After TriggeredFalseFalseTrueFalseTrueTrue
@AfterReturning TriggeredFalseFalseTrueFalseFalseFalse
Key Moments - 2 Insights
Why does @After run even if the method throws an exception?
@After is designed to run always after the method finishes, regardless of success or exception, as shown in execution_table rows 8 and 9.
Why doesn't @AfterReturning run when the method throws an exception?
@AfterReturning only runs if the method completes normally without exceptions, so it skips running in rows 8 and 9 where an exception occurs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does @AfterReturning advice run?
AStep 5
BStep 8
CStep 3
DStep 9
💡 Hint
Check the '@AfterReturning Triggered' column in execution_table rows.
At which step does the method throw an exception?
AStep 3
BStep 7
CStep 5
DStep 1
💡 Hint
Look at the 'Method Outcome' column for exception occurrence.
If the method never throws exceptions, which advice always runs?
A@AfterReturning only
B@After only
CBoth @After and @AfterReturning
DNeither advice
💡 Hint
Refer to execution_table rows 4 and 5 where both advices run after normal completion.
Concept Snapshot
@After and @AfterReturning in Spring AOP:
- @After runs always after method finishes (success or exception).
- @AfterReturning runs only if method returns normally.
- Use @After for cleanup tasks.
- Use @AfterReturning to access method return value.
- Both can target same methods with different triggers.
Full Transcript
This visual execution trace shows how Spring AOP's @After and @AfterReturning advices behave. When a method starts, it runs its logic. If it returns normally, both @After and @AfterReturning advices run after it finishes, printing messages. If the method throws an exception, only @After runs, while @AfterReturning does not. Variables track method outcome and advice triggers step-by-step. Key moments clarify why @After always runs and @AfterReturning only runs on success. Quiz questions test understanding of when each advice triggers. The snapshot summarizes their usage and differences.