Introduction to Spring Boot AOP

Introduction to Spring Boot AOP

In this article, I am going to discuss the Introduction to Spring Boot AOP. Please read our previous article where we discussed Spring Boot Eureka Client with Examples.

What is AOP?

AOP is Aspect-Oriented Programming. Aspects are special modularised classes that define layer-level responsibilities. As we know, an application is developed with several layers, such as:

  • Web layer
  • Business layer
  • Data layer

Each layer has some common responsibilities such as logging and security. These responsibilities are called cross-cutting concerns. If we implement each responsibility for each layer, the code will become more complex and hence difficult to maintain.

To solve this, aspect-oriented programming or AOP provides a solution to implement such responsibilities. This is how it can be done:

  • Implement cross-cutting concerns as an aspect
  • Define a point where this aspect has to be applied

This makes sure that cross-cutting concerns are defined together, which makes it easier to maintain. |One advantage of using AOP is that we are free to perform modifications to the code without having to worry about cross-cutting aspects.

AOP in Spring Boot

Spring Boot has an AOP Framework to implement AOP quickly. Cross-cutting concerns are defined as aspects. There are two main benefits of using aspects:

  1. The code for each concern is separate and secluded in one class, rather than being scattered across classes
  2. The business layer contains the code for primary concerns. The secondary concerns are moved to another class.

The aspects need to be implemented whenever they are required. The locations where aspects are implemented are called ‘join points’.

Benefits of using AOP in Spring Boot
  • It is implemented in pure Java.
  • There is no requirement for a special compilation process.
  • It supports only method execution Join points.
  • Only run-time weaving is available.
Some AOP Terminology

Some key terms in AOP are:

Term
Meaning
Aspect An aspect is a module that encapsulates advice and point cuts. It provides cross-cutting functionality. An application may have any number of aspects. To make a class into an aspect class, add the @Aspect annotation to the class.
Pointcut A pointcut is an expression that selects join points to execute the advice. We can define point cuts using expressions or patterns. In Spring, AspectJ is used.
Join Point A join point is a point in the application where we apply an AOP aspect. Or it can be a specific execution of an advice. In AOP, a join point can be a method execution, exception handling, changing object variable value, etc.
Advice The advice is an action that we take either before or after the method execution. An action is a piece of code that we invoke during program execution. There are five types of advices in the Spring AOP framework: before, after, after-returning, after-throwing, and around advice. Advices are taken for a particular join point.
Target Object An object on which advices are applied is called the target object. Target objects are always proxied. This means that if a subclass is created at runtime wherein the target method is overridden, the advices shall change according to the new configuration.
Weaving It is a process of linking aspects with other application types. We can perform weaving at run time, load time, and compile time. Run time is when an application is currently running. Load time is when an application has compiled and is loading into memory. Compile time is when the application is being compiled.
Proxy A proxy is an object that is created after applying advice to a target object. In Spring Boot’s implementation of AOP, the JDK dynamic proxy is used to create the proxy classes with target classes and advice invocations. These are called AOP proxy classes.
AOP vs OOP

AOP, as we know stands for Aspect Oriented programming. OOP stands for Object Oriented programming. Both of these are programming styles. The differences between them are as follows:

AOP:
  1. An aspect is a unit of code that contains pointcuts, advice, and attributes.
  2. A pointcut is a set of entry points in which an advice is executed.
  3. An advice is an implementation of cross-cutting concerns.
  4. A weaver is used to construct code with advice. The constructed code may be source code or object code.
OOP:
  1. A class is a unit of code that contains methods and attributes.
  2. A method signature defines entry points for the execution of method bodies.
  3. Method bodies are an implementation of business logic.
  4. A compiler is used to convert source code to object code.

A standard OOP implemented can be described using the following diagram:

Introduction to Spring Boot AOP

However, when OOP is combined with AOP, the diagram can be implemented as follows:

Introduction to Spring Boot AOP

Spring Boot AOP vs AspectJ

As mentioned above, Spring Boot’s implementation of AOP uses AspectJ. However, there are several key differences between the two:

Spring Boot AOP
  1. A separate compiler is not required.
  2. It supports only method execution pointcuts.
  3. It can only be implemented using beans.
  4. It supports only method-level weaving.
AspectJ
  1. A separate AspectJ compiler is required.
  2. It supports all pointcuts.
  3. It can be implemented on all domain objects.
  4. It can weave everything, i.e., fields, methods, constructors, etc.
Type of AOP Advices

As previously mentioned, AOP has 5 types of advices:

  1. Before Advice: An advice that executes before a join point, is called before advice. We use @Before annotation to mark an advice as Before advice.
  2. After Advice: An advice that executes after a join point, is called after advice. We use @After annotation to mark an advice as After advice.
  3. Around Advice: An advice that executes before and after of a join point, is called around advice.
  4. After Throwing Advice: An advice that executes when a join point throws an exception.
  5. After Returning Advice: An advice that executes when a method executes successfully.

In the next article, I am going to discuss Spring Boot AOP Before Implementation with Examples. Here, in this article, I try to explain the Introduction to Spring Boot AOP. I hope you enjoy this Introduction to Spring Boot AOP article.

Leave a Reply

Your email address will not be published. Required fields are marked *