@After and @AfterReturning help you run extra code after a method finishes. This is useful to add actions like logging or cleanup without changing the main method.
@After and @AfterReturning in Spring Boot
@After("pointcutExpression") public void afterAdvice() { // code to run after method } @AfterReturning(pointcut = "pointcutExpression", returning = "result") public void afterReturningAdvice(Object result) { // code to run after method returns successfully }
@After runs after the method finishes, whether it throws an error or not.
@AfterReturning runs only if the method finishes successfully and can access the returned value.
@After("execution(* com.example.service.*.*(..))") public void logAfter() { System.out.println("Method finished."); }
@AfterReturning(pointcut = "execution(* com.example.service.UserService.getUser(..))", returning = "user") public void logUser(Object user) { System.out.println("Returned user: " + user); }
This Spring Boot app has a service with two methods: process and getData. The aspect runs code after process finishes and after getData returns successfully. It prints messages to show when these happen.
package com.example.demo; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect @Component public class LoggingAspect { @After("execution(* com.example.demo.MyService.process(..))") public void afterProcess() { System.out.println("Process method finished."); } @AfterReturning(pointcut = "execution(* com.example.demo.MyService.getData(..))", returning = "result") public void afterReturningGetData(Object result) { System.out.println("getData returned: " + result); } } package com.example.demo; import org.springframework.stereotype.Service; @Service public class MyService { public void process() { System.out.println("Processing..."); } public String getData() { System.out.println("Getting data..."); return "Hello"; } } package com.example.demo; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan("com.example.demo") public class DemoApplication implements CommandLineRunner { private final MyService myService; public DemoApplication(MyService myService) { this.myService = myService; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void run(String... args) throws Exception { myService.process(); String data = myService.getData(); } }
Use @After when you want to run code no matter if the method succeeds or fails.
Use @AfterReturning when you need the method's return value to do something extra.
Make sure your pointcut expressions match the methods you want to advise.
@After runs code after a method ends, always.
@AfterReturning runs code only after a method returns successfully.
Both help add extra behavior without changing the original method.
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
