What is AOP and how to implement AOP in Spring Boot? In this article, we will understand Aspect-Oriented Programming in Spring boot. Prerequirements Spring Boot Tutorial.
There is a number of paradigm in the programming language on which any software or application is built. One is the Object-Oriented Programming language which every one of us is well aware of because we have learned C++, Java that is based on OOPs Paradigm. In the same way, we have a new paradigm called AOP i.e Aspect-Oriented Programming.
Note: Paradigm is nothing but the basic style of programming language.
The spring boot web application that is built contains many layers such as the API layer which has the endpoints for REST APIs, the business layer that contains the business logic, and the persistence layer that act as a data access layer.
Each layer has unique functionality, but there is some common functionality that is applied to all layers; we can call it an additional service such as Logging, Transaction, validation and etc. These additional services are nothing but cross-cutting concerns.
The process of moving these additional services to another class which is called Aspect and bind them when they are required is Aspect-Oriented Programming.
What is Spring Boot AOP?
Spring boot AOP also plays a vital role in the popularity of the spring framework. The Aspect-Oriented Programming uses aspects in the programming Just like OOPs uses Objects.
- In this paradigm, the code is broken down into different modules called cross-cutting concerns i.e Modularisation is achieved, where the key unit is called aspect. The cross-cutting concern is separated from the business logic.
- There are Aspects such as logging, transaction which are not considered as business logic but are required in the code.
- It is added by adding as advice to the existing code.
- AOP provides the pluggable way to add this additional concern before, after, or on the actual logic.
- And, hence we achieved modularisation using AOP.
Consider an example, you have to perform transaction management.
- When we perform a database operation, we have to begin the transaction, and after saving commit has to be done. Earlier we use to write these inside the business class only.
- But, now we will follow the AOP, implement cross-cutting concerns. i.e move these additional services to another class called as aspect.
- If we use the AOP paradigm, then the business model of your program will not know about transactions and you can plug using the AOP aspect wherever you want.
Let us see some important terminology related to Spring Boot AOP.
Important Terminology of Spring Boot AOP
Aspect: Aspect is a class that contains cross-cutting concerns i.e the additional service like transaction, logging, validation, and etc. This class is annotated by @Aspect.
@Aspect public class LogService{ //code begins here... }
Advice: It is the method inside the aspect class that provides the actual implementation.
@Aspect public class LogService{ public void startsLog() { System.out.println("LOG STARTS.."); } }
Pointcut: It is an expression that will select the business method which needs advice. It is defined using expression.
Joinpoint: It is a point in the application where the classes/business method links with pointcut.
Target: An object that is being advised by one or more aspects is Target Object.
Weaving: Weaving links aspects with other application types or objects to create an advised object. Spring AOP performs weaving at runtime.
Proxy: It is the final output that contains advice and target objects. Spring boot uses JDK Dynamic proxy to create proxy classes.
Types of Advice:
- Before: It executes before the joint point. It is denoted by @Before
- After: It executes after the joining point regardless of the outcomes. It is denoted by @After.
- After running: It executes after the successful running of joinpoint. It is denoted by @AfterRunning.
- Around: It executes before and after the joinpoint. It is denoted by @Around.
- After Throwing: It executes after the joining point throws Runtime Exception. It is denoted by @AfterThrowing.
In order to work with AOP in the spring boot application, we need to add the spring AOP dependency in the po.xml file:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
How to implement AOP in Spring Boot?
we can implement AOP by using the following ways:
- By using XML Based configuration.
- By using AspectJ (Annotation)
Note: In Spring boot, we will use the Annotation-based configuration.