@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.