Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Spring Boot AOP After Advice Setup
1. Project Initialising: 
2. Adding Dependency: 
2. Enable Spring Aspects: 
Spring Boot AOP After Advice Example
3.1. : 
3.2. :
3.3. :
3.4. :
Key Takeaways
Last Updated: Mar 27, 2024

Spring Boot AOP After Advice

Author Tanay Kumar
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM


Spring Boot's AOP( Aspect-Oriented Programming ) is a programming pattern that allows cross-cutting concerns. The cross-cutting concern is different from the main application logic. We can add additional functionality to our application without modifying the code. 

This article will focus on the Spring Boot AOP after advice. After, advice is used to achieve the cross-cutting. This advice ensures that advice runs after executing the target method. This advice is similar to the finally block in Java that it always executes irrespective of the outcome.

We can use Spring Boot AOP After advice for logging and resource cleaning. And @After notation to implement the Spring Boot AOP after advice. Let's understand after advice with an example. 

Spring Boot AOP After Advice Setup

This section will set up all the necessary components before moving on with the example.

1. Project Initialising: 

First of all, we will initialize the project by visiting


As seen in the above image, we will name our group as com.codingninjas, Artifact as aop-after-advice. We will also add Spring Web dependency in our project. Now, Let's click on GENERATE button to wrap all the specifications and download the jar file.

2. Adding Dependency: 

To use Spring Boot AOP after advice, we need to add the spring-boot-starter-aop dependency. We will add this Spring Boot starter by writing the following code in our pom.xml file.



2. Enable Spring Aspects: 

While developing an application in Spring, we need to enable the AOP module explicitly. We will achieve this by writing the following code in the file.


public class AopAfterAdviceApplication   
      public static void main(String[] args) {  
  , args);  


The above code enables support for handling components marked with @Aspect annotation. We use it with @Configuration annotation. We can control the proxy type using the proxyTargetClass attribute (default value is false).

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Spring Boot AOP After Advice Example

After doing the initial setup for the Spring Boot AOP after advice, let's proceed with the example. First, let's create a class named in the package com.codingninjas.model. : 

In the class, we will define the following:

  • Variable empName, empID, empAge.
  • Generate getters and setters for the variable.
  • Create a default constructor.
package com.javatpoint.model;  
public class Employee  {  

    private String empId;  
    private String empName;  
    private int empAge;  

    // Default constructor  
    public Employee()   {  

            // Getters and Setters
    public String getEmpId() {  
          return empId;  
    public void setEmpId(String empId) {  
          this.empId = empId;  
    public String getEmpName()  {  
          return empName;  
    public void setEmpName(String Name)  {  
          this.empName = Name;  
    public int getEmpAge()   {  
          return empAge;  
    public void setSecondName(int age)  {  
          this.empAge = age;  


After successfully creating the Employee Class, let's create a controller class under the package com.codingninjas.controller. :

In the controller class, we will define the following:

  • Mapping for adding an employee.
  • Mapping for removing an employee.
package com.codingninjas.controller;   
import org.springframework.web.bind.annotation.RestController; 
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;    
import org.springframework.web.bind.annotation.RequestParam; 

import com.codingninjas.model.Employee;  
import com.codingninjas.service.EmployeeService;  

public class EmployeeController   {  

    private EmployeeService employeeService;  

    @RequestMapping(value = "/add/employee", method = RequestMethod.GET)  
    public com.codingninjas.model.Employee addEmployee(@RequestParam("empId") String empId, @RequestParam("empName") String empName, @RequestParam("empAge") int empAge)  {  
          return employeeService.createEmployee(empId, firstName, secondName);  

    @RequestMapping(value = "/remove/employee", method = RequestMethod.GET)  
    public String removeEmployee( @RequestParam("empId") String empId)   
          return "Employee removed successfully";  


We successfully created the mapping for adding and removing an employee in the class in the above code. Now, let's make the service class for creating and deleting employees. :

This class is created under the com.codingninjas.service package. In this class, we will define the following:

  • Method to create an employee.
  • Method to delete an employee.
package com.codingninjas.service;   
import org.springframework.stereotype.Service; 
import com.codingninjas.model.Employee;

public class EmployeeService   {  

       public Employee createEmployee( String empId, String empname, int empage) {  
             Employee emp = new Employee();  
             return emp;  

       public void deleteEmployee(String empId)   {  



Now, we will create the under the package com.codingninjas.aop :

This class will define the Spring Boot AOP after advice logic.

package com.codingninjas.aop;  
import org.aspectj.lang.annotation.Aspect;   
import org.springframework.stereotype.Component; 
import org.aspectj.lang.JoinPoint;  
import org.aspectj.lang.annotation.After;   

public class EmployeeServiceAspect   
      @After(value = "execution(* com.codingninjas.service.EmployeeService.*(..)) and args(empId, empname, empage)")  
      public void afterAdvice(JoinPoint joinPoint, String empId, String empname, int empage){  
            System.out.println("After method:" + joinPoint.getSignature());  
            System.out.println("Creating Employee with name - " + empname + ", age - " + empage + " and id - " + empId);  


In the above class, we used the following:

  • execution(exp): The exp is a method where we will apply after advice.
  • @After: The method with @After annotation executes after all the matching methods with the pointcut expression.

Now that we have successfully built our application let's run the application by clicking on and selecting Run As > Java Application

Open the browser and open the following URL: http://localhost:8080/add/employee?empId={id}&empName={empname}&empAge={empage}


Let's have a look at the console. We see that @after invoking the createEmployee() method of class, the method afterAdvice() of class invokes, as shown below.

2022-01-09 19:11:28.016  INFO 952 --- [main] o.s.web.servelet.Dispatcherservelet


After method: Employee com.codingninjas.service.EmployeeService.createEmployee(String, String, int)

Creating Employee with name - Coding Ninjas, age - 8 and id - 101


Similarly, we can also remove the employee by invoking the URL http://localhost:8080/remove/employee?empId=101. It returns a message Employee removed successfully, as shown in the below image.



  1. What is a Spring Boot AOP?
    Spring Boot's AOP( Aspect-Oriented Programming ) is a programming pattern that allows the cross-cutting concern in the application.
  2. What are the different Spring Boot AOP advice?
    Spring Boot advice is associated with a pointcut expression and runs before, after, or around method executions matched by the pointcut. Spring Boot offers the following AOP advice: Before adviceAfter returning adviceAfter throwing adviceAfter (finally) adviceAround adviceAdvice parameters, and Advice ordering.

Key Takeaways

In this article, we learned about spring boot AOP after advice. We implemented AOP After advice with a detailed example and learned how to use it.

Don't stop here. Check out the articles on Spring Boot Auto-configuration, Spring Boot AnnotationsSpring Boot Multi-Module ProjectSpring Boot Packaging, and Spring Boot CLI.

We hope you found this blog helpful. Liked the blog? Then feel free to upvote and share it.

Live masterclass