Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why AOP
3.
Definition of Spring Boot AOP:
4.
Cross-Cutting Concerns:
5.
Benefits of Spring Boot AOP:
6.
Aspect:
7.
Benefits of Aspect:
8.
Terminologies related to Spring Boot AOP:
9.
AOP Vs. OOP
10.
Types of Spring Boot AOP Advice
11.
FAQs
12.
Key Takeaways:
Last Updated: Mar 27, 2024

Spring Boot AOP

Author Gunjan Ahuja
2 upvotes
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

We all want to write an easy-to-understand code that is readable and lightweight. Therefore, in this blog, we will understand the concept of Aspect-Oriented Programming(AOP). Spring boot AOP enables Aspect-Oriented Programming in Spring Applications. The AOP concept is very easy to implement, and with this, we can add modularity to our code. 

There is no need to recompile the complete code if we have made any manipulations. Instead, we just need to change the configuration files to make it done. So, let's read and understand the concept of AOP with the help of this blog.

Why AOP

By simply using simple pluggable configurations, we can dynamically add the cross-cutting concern before, after, or around the actual logic. Moreover, maintainability is achieved for the present as well as the future. Therefore, AOP plays a very crucial role.

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
Bootcamp

Definition of Spring Boot AOP:

AOP is known as Aspect-Oriented Programming. It adds modularity to the program without modifications in the original code itself. Instead, we can achieve modularity with the separation of the cross-cutting concerns. The central unit of modularity is an aspect.

The extra functionality can be added before or after the method execution whenever a method gets executed.

Cross-Cutting Concerns:

A cross-cutting platform is something that we can implement over the code core without any modification. We can add this concern at multiple places in the application.
For example, security is a cross-cutting concern that must be implemented over various platforms. So, despite repeatedly adding the code at every method, we can define the functionality in a standard class and control where we need to apply that functionality in the whole application. Some common cross-cutting concerns are logging, validation, and caching.

Benefits of Spring Boot AOP:

  • It's like plug-and-play. There is no need to touch core code or business logic.
  • It's effortless to configure the model.
  • In pure java, we can implement AOP.
  • As regular spring beans, we can configure the aspects.
  • The cross-cutting concerns are stored in one place only.
  • The logic of each concern is at one place rather than being scattered.

Aspect:

Like in Object-Oriented Programming, there is a class in the same in Aspect-Oriented Programming, there is an aspect. Aspect is a special kind of class where the cross-cutting concern can also be modularized.

We need to implement a cross-cutting concern with the help of aspects.

Benefits of Aspect:

  • In one place, we can store the logic of concern. Therefore, the readability of the code is maintained.
  • The primary concerns are held in the business logic. At the same time, the secondary problem is stored in aspect. Therefore, clarity is optimized.

Terminologies related to Spring Boot AOP:

Let's have more clarity about AOP with the help of the following terminologies:

  • ASPECT: It is a module with a set of APIs(Application Programming Interface) that provide all cross-cutting requirements. For example, a security module would be called the AOP aspect for security alignment. There can be any number of aspects depending upon the need.
  • JOIN POINT: It is the actual place in the application where an action will be taken using the Spring AOP framework.
  • POINT CUT: It is the expression language of the AOP. A pointcut combines two or more joinpoints where advice needs to be executed. It can be implemented using expressions or patterns.
    ADVICE: Advice is nothing but an action that we will take before or after implementing the method. Basically, they are the method that gets executed when a certain join point needs a matching point cut in the application. The five types of advice are as follows:  before, after, after-returning, after-throwing, and around advice. 

Example: The following diagram shows the execution of advice.

Execution of advice

Remember, advice can't be for every join point. It changes with the join point.

  • TARGET OBJECT: Target objects are those over whom the advice is implemented.
  • WEAVING: Weaving is the process that links aspects with other application types or objects to create an advised object. It can be implemented anytime during runtime, load time, or compile time. Specifically, spring AOP executes the weaving process during runtime.

AOP Vs. OOP

AOP can't replace OOP; instead, it just extends it.

Let's understand the difference between AOPs and OOP's.

Factors AOP OOP
Definition AOP stands for Aspect-Oriented Programming. OOP stands for Object-Oriented Programming.
Code Creation Code creation is apparent and evident. Code creation is not that clear; instead, there is functionality within the class.
Modularity The basis of modularity is functionalities or processes. The basis of modularity is real-world objects.
Reusability Reusability of code is to a greater extent. Reusability of code is to a lesser extent.

 

Let's take a look at the diagrams:

AOP


OOP

Types of Spring Boot AOP Advice

Below is the detail of five kinds of advice:

  • Before Advice: This gets implemented before the joining point.
  • After Returning Advice: It gets executed once the joining end gets completed typically.
  • After Throwing Advice: It runs if the method exits by throwing an exception.
  • After Advice: It confirms it gets executed whether the joining point usually leaves or exceptionally gets returned.
  • Around Advice: It runs after and before a join point.

FAQs

1. Which problems are solved with the help of Spring Boot  AOP?

Ans: Spring Boot  AOP solves the problem of code tangling and eliminates code scattering.

Code is accumulated in a particular module which can be implemented anytime in the code that’s how the code becomes clean and readable.

 

2. Name three types of cross-cutting concerns.

Ans: The three cross-cutting concerns are:-

  • Logging
  • Security
  • Transaction

 

3. Differentiate concern and cross-cutting concern

Ans: The following shows the difference between concern and cross-cutting concern.

  • Concern: It is the behavior that we want in the module of the application
  • Cross-cutting concerns: It is applicable throughout the application.

Key Takeaways:

This was all about AOP. When we face interview questions, this basis of knowledge of Spring Boot AOP will be helpful. Please make sure that you correctly understand the basics over which the whole Spring Boot AOP is based and the terminologies associated with it. Since it is user-friendly and it becomes easier to understand, that's why this model is preferable. 

We have more blogs for Spring Boot AOP before Advice and Spring Boot AOP after advice and a few more basic concepts of it as per the usability. 

You can also consider our Spring Boot Course to give your career an edge over others.

Next article
Spring Boot AOP Before Advice
Live masterclass