0
0
Spring Bootframework~5 mins

@After and @AfterReturning in Spring Boot

Choose your learning style9 modes available
Introduction

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

You want to log a message every time a method ends, no matter if it worked or threw an error.
You want to do something only after a method successfully returns a result.
You want to clean up resources after a method runs.
You want to track method execution for debugging or monitoring.
You want to add behavior after a method without changing its code.
Syntax
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.

Examples
This runs after any method in the service package finishes, printing a message.
Spring Boot
@After("execution(* com.example.service.*.*(..))")
public void logAfter() {
    System.out.println("Method finished.");
}
This runs only after getUser method returns successfully and prints the returned user.
Spring Boot
@AfterReturning(pointcut = "execution(* com.example.service.UserService.getUser(..))", returning = "user")
public void logUser(Object user) {
    System.out.println("Returned user: " + user);
}
Sample Program

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.

Spring Boot
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();
    }
}
OutputSuccess
Important Notes

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.

Summary

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