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

Step 2: Add AOP Dependency

Add Spring AOP dependency in pom.xml.

pom.xml

XML
<project xmlns="https://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 
                             https://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.before_advice</groupId>
 <artifactId>aop-before-advice-example</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 
 <packaging>jar</packaging>  

 <name>aop-before-advice-example</name>
 <description>Demo project for Spring Boot</description>

  <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.2.2.RELEASE</version>
     <relativePath /> <!-- lookup parent from repository -->
  </parent>

  <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
     <java.version>1.8</java.version>
  </properties>

    <dependencies>
       <!-- dependency for spring web -->
       <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
 
       <!-- added dependency for spring aop -->
       <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
    </dependencies>

<build>
 <plugins>
  <plugin>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
  </plugin>
 </plugins>
</build>

</project>

Step 3: Create Model Class

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

Student class:

Java
package com.after_returning_advice.model;

public class Student {
    private String firstName;
    private String secondName;

    public Student() {
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getSecondName() {
        return secondName;
    }

    public void setSecondName(String secondName) {
        this.secondName = secondName;
    }
}

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:

Java
package com.after_returning_advice.service;

import org.springframework.stereotype.Service;
import com.after_returning_advice.model.Student;

@Service
public class StudentService {
    public Student addStudent(String fname, String sname) {
        System.out.println("Add student service method called");
        Student stud = new Student();
        stud.setFirstName(fname);
        stud.setSecondName(sname);
        if(fname.length()<=3)
            throw new RuntimeException("Length of firstname must be 4 or more" );
        return stud;
    }
}

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:

Java
package com.after_returning_advice.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.after_returning_advice.model.Student;
import com.after_returning_advice.service.StudentService;

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping(value = "/add")
    public Student addStudent(@RequestParam("firstName") String firstName, @RequestParam("secondName") String secondName) {
        return studentService.addStudent(firstName, secondName);
    }
}

 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:

Java
package com.after_returning_advice.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class StudentServiceAspect {
    // the pointcut expression specifying execution of any
    // method in com.after_returning_advice.service.StudentService
    // class of any return type with 0 or more number of arguments
    @Pointcut("execution(* com.after_returning_advice.service.StudentService.*(..)) ")
  
      // the pointcut signature
      private void anyStudentService() {}
    
    @AfterReturning("anyStudentService() && args(fname, sname)")
    public void afterReturningAdvice(JoinPoint joinPoint, String fname, String sname) 
    {
        System.out.println("After Returning method:" + joinPoint.getSignature()+"\n "
                + "Added Student with first name - " + fname + ", second name - " + sname );    
    }
}

Step 7: Run the Application

Run the project as a Spring Boot Application.

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

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

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

Comment