In Spring AOP, After Advice is used to execute code after a target method finishes execution. It is defined using the @After annotation. The advice method runs regardless of whether the method executes successfully or throws an exception. This makes it useful for tasks like logging, resource cleanup, or auditing after method execution.
- @After advice runs after the execution of a target method (join point).
- It executes whether the method completes normally or throws an exception.
- It is commonly used for logging, auditing, or releasing resources.
Syntax:
@After("execution(* package_name.class_name.method_name(..))")
public void afterAdviceMethod(JoinPoint joinPoint) {
// code to run after method execution
}
Steps to Implement After Advice in Spring Boot
Step 1: Create a Spring Boot Project
Open Spring Initializr and provide the following details:
- Group: com.after_advice
- Artifact: aop-after-advice-example
- Dependencies: Spring Web
Download the project and extract the ZIP file.
Step 2: Import the Project into IDE
Import the project using: File -> Import -> Existing Maven Project -> Select Project Folder -> Finish.
pom.xml file:
<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>
Note: If the jars are not added properly, you may get some errors.
Step 3: Create Model Class
Create package com.after_advice.model and add Student.java class to store student details such as firstName and secondName.
Student class:
package com.after_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_advice.service and add StudentService.java. This class contains the addStudent() method which creates and returns a student object.
StudentService class:
package com.after_advice.service;
import org.springframework.stereotype.Service;
import com.after_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_advice.controller and add StudentController.java. This controller handles GET request /add and calls the StudentService method.
StudentController class:
package com.after_advice.controller;
import com.after_advice.model.Student;
import com.after_advice.service.StudentService;
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;
@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 7: Create Aspect Class
Create package com.after_advice.aspect and add StudentServiceAspect.java.
- Define a Pointcut to target service methods.
- Add @After advice so that the advice runs after the service method execution.
StudentServiceAspect class:
package com.after_advice.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
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_advice.service.StudentService
// class of any return type with 0 or more number of
// arguments
@Pointcut("execution(* com.after_advice.service.StudentService.*(..)) ")
// the pointcut signature
private void anyStudentService() { }
@After("anyStudentService() && args(fname, sname)")
public void afterAdvice(JoinPoint joinPoint,
String fname, String sname)
{
System.out.println(
"After method:" + joinPoint.getSignature()
+ "\n "
+ "Added Student with first name - " + fname
+ ", second name - " + sname);
}
}
Step 8: Run the Application
Run the project as Spring Boot Application and open the browser.
Example URL:
For a demo, we are hitting URL with fname as Harry and sname as Potter. In this case, the method will be executed normally.
Output Explanation
Case 1: Normal Execution
- StudentService.addStudent() method executes and creates the student object.
- @After Advice runs after the method execution.
- The controller returns the student details as the response.
Case 2: Exception Occurs
- The service method throws an exception (for example when firstName = Tom).
- Even though an exception occurs, @After Advice still executes.
- The advice logs the method execution or performs cleanup tasks.
When we hit URL with fname as Tom, the service method will throw an exception. The After advice will be executed.

As seen in the output, the advice method is called after the service method is executed successfully or any exception is raised.