Spring Boot AOP Implementation

Updated: Jul 5


Spring Boot AOP implementation main image

In this blog, we will implement Aspect-Oriented Programming in Spring Boot Service.


I will highly recommend checking out the other two introductory blogs.


This blog is the last part of the three-part blog series.


We will learn AOP Implementation by developing logging functionality.


Implementation

We need to perform four easy steps to create an aspect class.

  • Add Dependency

  • Annotate your class

  • Define pointcut

  • Define Advice

STEP 1: Add Dependency

First, we need to add a spring-boot-starter-aop dependency in our pom.xml


STEP 2: Annotate your class

  • Create a Class.

  • Apply @Aspect annotation over a class that will mark a class as Aspect (class containing advice)


STEP 3: Define Pointcut

A pointcut is a set of joinpoints. Here we define the expression, that will specify which methods will execute advice.


There are different pointcut expressions that can be used to create joinpoints.


Pointcut expressions are defined using Pointcut Designator (PCD), which represents a joinpoint or set of joinpoints.

  • execution: pointcut designator for creating method execution as a joinpoint. It will execute the Advice of Aspect Class on Method (which is defined as a joinpoint by PCD) Execution.

  • within: pointcut designator for creating a method of a class as a joinpoint. It will execute the Advice of Aspect Class on Method Execution of a class (which is defined as a joinpoint by PCD).

  • @within: pointcut designator for creating a method of an annotated class ( e.g., Repository Class, Service Class, Component Class ) as a joinpoint. It will execute the Advice of Aspect Class on Method Execution of a class (which is defined as a joinpoint by PCD).

  • target: pointcut designator for creating joinpoint where the target object is an instance of the given type.

  • @target: pointcut designator for creating joinpoint where the target object has an annotation of a given type.

  • this: pointcut designator for creating a joinpoint where the bean reference is an instance of a given type.

  • args: pointcut designator for creating joinpoint where the method parameters is an instance of a given type.

  • @args: pointcut designator for creating joinpoint where the method parameter has an annotation of a given type.

  • @annotations: pointcut designator for creating joinpoint where the subject (subject can be a class or method) has an annotation( e.g., Repository Class, Service Class, Component Class).


Some Important points to remember

Remember these pointers, if you have to choose between different PCDs.

  • If you have to choose between execution PCD and within PCD, always use within PCD, since it occupies less heap memory because execution PCD creates interceptor for each matched method where within PCD will create one interceptor. Check out this StackOverflow.

  • @target and @within PCD can be used interchangeably in Spring AOP. In AspectJ, they behave differently. Check out this StackOverflow.


STEP 4: Define Advice

Annotate your methods with @Before, @AfterRetruning, @Around, and @AfterThrowing depending upon when you want to execute it. Check out my previous blog, to know more about, when each piece of advice gets completed.


@Before Advice


Below Advice will be executed just before any repository methods calls and will capture method name, class name, method parameters.


@AfterReturning Advice


Below advice will be executed when the function returns and capture the method name, class name and return value of the repository methods.


@Around Advice


Below advice will be executed during the repository method execution and captures method name, class name, and execution time in millis.



@AfterThrowing Advice


Below advice will be executed when ay repository method throws an error, and captures method name, class name, request body, and stack trace.

Examples

We will check out two examples

  • Logging using Custom Annotation

  • Logging without Custom Annotation

With Custom Annotation

In the below example, Loggable.java is the custom annotation class, and LoggableAspect.java contains all advice, which gets executed, whenever your program finds @Loggable over any methods.


Sample Usage


Without Custom Annotation

In the below example, methods of all classes annotated with @Component will be logged automatically.


I hope this blog helps you to develop an understanding of Aspect-Oriented Programming Implementation in Spring Boot.


Comments and feedback are most welcomed.

Please follow me on Linkedin, Github, Medium to keep yourself updated.


Thanks for reading. Happy Learning 😊

401 views

Recent Posts

See All