VOOZH about

URL: https://www.geeksforgeeks.org/java/spring-boot-aop-after-returning-advice/

⇱ Spring Boot - AOP After Returning Advice - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring Boot - AOP After Returning Advice

Last Updated : 23 Mar, 2026

After Returning Advice runs only after a method executes successfully. It is implemented using the @AfterReturning annotation.
If the method throws an exception, this advice will not execute.

  • Used to execute logic after successful method completion.
  • Defined using the @AfterReturning annotation in an aspect class.
  • Commonly used for logging results, auditing, or monitoring.

Steps to Implement After Returning Advice

Step 1: Create Spring Boot Project

Open Spring Initializr and provide the following details:

  • Group: com.after_returning_advice
  • Artifact: aop-after-returning-advice-example
  • Add dependency: Spring Web

Download the project and extract the ZIP file and import the Project into IDE

👁 Image

Step 2: Add AOP Dependency

Add Spring AOP dependency in pom.xml.

pom.xml

Step 3: Create Model Class

Create package: com.after_returning_advice.model and add Student class to represent student data.

Student class:

Step 4: Create Service Class

Create package: com.after_returning_advice.service and Add StudentService class with a method to add students using name arguments

StudentService class:

Step 5: Create Controller Class

Create package: com.after_returning_advice.controller and add StudentController to handle GET requests and call the service method

StudentController class:

 Step 6: Create Aspect Class

Create package: com.after_returning_advice.aspect and add StudentServiceAspect class.
Define:

  • Pointcut expression
  • Advice method using @AfterReturning
    This advice runs only after the service method executes successfully. 

StudentServiceAspect class:

Step 7: Run the Application

Run the project as a Spring Boot Application.

👁 Image

For the demo, we are hitting URL with fname as Harry and sname as Potter. In this case, the method will be executed normally.

👁 Image

When we hit URL with fname as Tom, the service method will throw an exception. The After Returning advice will not be executed.

👁 Image
👁 Image

As seen in the output, after returning advice is called after the service method is executed successfully.

Comment