VOOZH about

URL: https://www.geeksforgeeks.org/java/how-to-implement-aop-in-spring-boot-application/

⇱ How to Implement AOP in Spring Boot Application - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Implement AOP in Spring Boot Application

Last Updated : 5 May, 2026

Aspect-Oriented Programming (AOP) in Spring Boot is used to separate cross-cutting concerns like logging, security, and transaction management from business logic. It improves code modularity by applying these concerns across multiple layers without modifying the core functionality. Spring Boot simplifies AOP integration using annotations and auto-configuration.

  • Enables separation of cross-cutting concerns such as logging, security, and transactions.
  • Uses annotations like @Aspect, @Before, @After, @Around, and @Pointcut.
  • Implemented using proxy-based mechanism (JDK dynamic proxies or CGLIB).
  • Easily enabled in Spring Boot using @EnableAspectJAutoProxy or auto-configuration.

Types of AOP Advice in Spring

👁 Advice-in-AOP

1. @Around

It executes both before and after the target method. The first parameter must be of type ProceedingJoinPoint, and it must invoke the proceed() method to continue the target method’s execution.

Use Cases:

  • Track execution time of methods
  • Centralized logging before and after service calls
  • Profiling or performance monitoring

2. @Before

Executes before the target method is invoked. If @Around advice exists, its “before” portion executes first, followed by @Before.

Use Case: Performing input validation or pre-condition checks before a method executes.

3. @After

Executes after the method execution, regardless of its outcome (successful or exception). If @Around advice exists, its “after” part executes first.

Sample code snippet:

Use Case: Performing resource cleanup, sending notifications, or closing database connections.

4. @AfterReturning

Executes only when the method successfully returns a result. It can capture the return value of the method.

Use Case:Logging successful operations or triggering dependent actions after a successful method call.

5. @AfterThrowing

Executes only when a method throws an exception. It helps log or handle exceptions globally.

Use Case: Error logging, alert notifications, or rollback operations in case of failure.

Steps to Implement AOP in Spring Boot Application

Step 1: Create Spring Boot Project

  • Create a Spring Boot project using Spring Initializr or IDE.
  • Add dependencies: Spring Web and Spring AOP.

Project Structure:

👁 Project Structure
Project Structure

Step 2: Configure pom.xml

  • Add spring-boot-starter-aop for AOP support.
  • Ensure Spring Boot parent and plugins are properly configured.

pom.xml

Step 3: Create Service Class

  • Create a service class (ServiceExample) with business methods.
  • These methods act as target methods where AOP advice will be applied.

ServiceExample.java

Step 4: Create Aspect Class

  • Create a class annotated with @Aspect and @Component.
  • Define a Pointcut to target service methods. Add different types of advice:
  • @Before : runs before method execution
  • @After : runs after execution
  • @AfterReturning : runs after successful execution
  • @AfterThrowing : runs on exception
  • @Around : runs before and after (full control)

ImplementationOfDifferentAspect.java

Step 5: Enable AOP

  • Add @EnableAspectJAutoProxy in the main class.
  • This enables Spring AOP proxy mechanism.

ImplementationOfDifferentAdvice.java



Step 6: Run the Application

  • Start the Spring Boot application.
  • Get the service bean from the context and call methods.

Output:

👁 Output
Output

Step 7: Observe Output

AOP advices execute automatically:

  • Logs before method execution.
  • Logs after execution.
  • Logs on success or exception.
Comment
Article Tags: