Discover how to run code after any method finishes without cluttering your main code!
Why @After and @AfterReturning in Spring Boot? - Purpose & Use Cases
Imagine you have many methods in your application, and you want to run some code after each method finishes, like logging or cleaning up resources.
You try to add this code manually inside every method.
Manually adding the same code after every method is boring, error-prone, and makes your code messy.
If you forget to add it somewhere, your app might behave inconsistently.
@After and @AfterReturning let you write that extra code once and have it run automatically after methods finish, keeping your code clean and consistent.
public void someMethod() {
// method logic
System.out.println("Method finished");
}@After("execution(* com.example..*(..))") public void afterAdvice() { System.out.println("Method finished"); }
You can add behavior after methods run without touching their code, making maintenance easier and your app more reliable.
Logging user actions after every service method completes, without adding logging code inside each method.
@After and @AfterReturning run code after methods finish.
They keep your code clean by separating extra tasks from main logic.
This helps maintain consistency and reduces errors.
Practice
@After and @AfterReturning in Spring AOP?Solution
Step 1: Understand
@Afterbehavior@Afteradvice runs after a method finishes, whether it returns normally or throws an exception.Step 2: Understand
@AfterReturningbehavior@AfterReturningadvice runs only if the method completes successfully without throwing exceptions.Final Answer:
@Afterruns after method completion regardless of outcome;@AfterReturningruns only after successful return. -> Option AQuick Check:
@After= always after,@AfterReturning= after success [OK]
- Confusing @After with @Before advice
- Thinking @AfterReturning runs on exceptions
- Believing both advices behave identically
@AfterReturning advice method in Spring AOP?Solution
Step 1: Check required attributes for @AfterReturning
The@AfterReturningannotation requires apointcutexpression and optionally areturningattribute to capture the returned value.Step 2: Match method signature with returning attribute
The method must have a parameter matching the name inreturningto receive the returned value.Final Answer:
@AfterReturning(pointcut = "execution(* com.example.*.*(..))", returning = "result") public void afterReturn(Object result) { } -> Option AQuick Check:
Use both pointcut and returning with matching parameter [OK]
- Omitting pointcut attribute
- Not matching returning name with method parameter
- Using @AfterReturning without parameters
someMethod() throws an exception?
@After("execution(* com.example.Service.someMethod(..))")
public void afterAdvice() {
System.out.println("After advice executed");
}
@AfterReturning(pointcut = "execution(* com.example.Service.someMethod(..))", returning = "result")
public void afterReturningAdvice(Object result) {
System.out.println("AfterReturning advice executed with: " + result);
}Solution
Step 1: Analyze
@Afteradvice behavior on exception@Afteradvice runs after method finishes regardless of success or exception, so it will execute and print its message.Step 2: Analyze
@AfterReturningadvice behavior on exception@AfterReturningadvice runs only if the method returns successfully, so it will NOT run if an exception is thrown.Final Answer:
After advice executed -> Option DQuick Check:
@After runs always, @AfterReturning skips on exceptions [OK]
- Assuming @AfterReturning runs on exceptions
- Thinking both advices run always
- Confusing advice execution order
@AfterReturning(pointcut = "execution(* com.example.Service.process(..))", returning = "retVal")
public void afterReturn() {
System.out.println("Returned value: " + retVal);
}Solution
Step 1: Check method parameters for returning attribute
Thereturning = "retVal"attribute requires the advice method to have a parameter namedretValto receive the returned value.Step 2: Verify method signature
The method currently has no parameters, so it cannot accessretVal, causing a compile error.Final Answer:
The advice method must have a parameter named retVal to receive the returned value. -> Option CQuick Check:
Returning attribute needs matching method parameter [OK]
- Omitting method parameter for returning value
- Misnaming the parameter
- Using @AfterReturning without pointcut
Solution
Step 1: Log returned value only on success
@AfterReturningadvice runs only after successful method return, so it is suitable to log the returned value.Step 2: Log after method finishes regardless of outcome
@Afteradvice runs after method completion whether success or exception, so it is suitable to log a message always.Final Answer:
Use@AfterReturningto log the returned value and@Afterto log after method completion. -> Option BQuick Check:
AfterReturning = success only, After = always after [OK]
- Using @After for returned value logging
- Using @Before to log returned value
- Expecting @AfterReturning to run on exceptions
Ready to prove it?
Pass the test and this pattern moves unknown
