Introduction to Spring AOP
AOP (Aspect Oriented Programming) introduces a new way of visualizing the programming structure. Like, OOP (Object-Oriented Programming), whose main unit of modularity is class, the unit of modularity for AOP is an aspect. Spring Framework also provides Aspect-Oriented Programming by implementing AOP concepts. Spring AOP is widely used as an implementation of cross-cutting concern i.e., a module or a functionality which is defined in one place but needed in many places across the project.
In simple terms, the cross-cutting concern is something that is centralized in one place of the project & used across multiple places such as Logging, Authentication, Security, Transaction Management, etc.
Need for Spring AOP
To understand the need for Spring AOP in a better way, let’s look at a problem statement in brief & also how Spring AOP resolved it.
Let’s say we have around 5 methods in a service layer & we need to perform some notification when a method is called as well as when the control exits the method.
To achieve this, we need to call the notification methods during the start & end in all the 5 methods in the service layer.
So, this is a tedious & repetitive work of writing the same code again & again in all the 5 methods. Also, in the future, if we wanted to remove the notification event for one of the methods, we need to remove the code again. So, there will a problem with maintenance too.
So, if you notice in TransactionService class, for each & every method we are calling startService() and endService() method from NotificationService. This acts as a boilerplate coding & repetitive work for the developers. Also, if the client requires to stop calling the NotificationService for emailService() and loggingService(), again we need to remove the code from TransactionService class.
With the help of Spring AOP, the aspect of calling the NotificationService during the start & end of each method can be centralized which reduces the repetitive work & also will be a good fit for the future with less maintenance.
So, Spring AOP dynamically provides a way to add cross-cutting concerns (i.e., calling the Notification Service in this case) using simple pluggable XML Configuration files or by using Java Annotations.
Spring AOP Terminologies
Some of the terminologies of Spring AOP are as follows.
It would be difficult to understand the above terminologies theoretically. So, let us witness a practical example of using Spring AOP and how the terminologies are interconnected.
Spring AOP Code Snippet
Transaction Service (package: com.muneer.service)
From the above code snippet, Logging Management needs to be achieved during the start & completion for each & every method in TransactionService.
If we need to write loggers during the start & end of each method in the business layer, that would be a tedious task. So, we are assigning the logging activities to be taken care of by Spring AOP.
So, we are making use of Spring AOP to centralize the Logging Management.
Aspect is nothing but the concern or the functionality that you are trying to implement generally or centrally.
From the above example, the aspect is Logging Management.
Some of the aspects used across the industry are Logging Management, Transaction Management, Exception Handling, Performance Metrics, Authentication, Security, etc.,
In Simple terms, aspect is the functionality that you are trying to achieve through Spring AOP.
A pointcut is nothing but, a kind of regular expression that specifies, what are the method calls that need to be intercepted.
From the code snippet, Line no. 24 & 30 in AspectConfiguration,
The Regular expression denoting that all the methods (using *.*) in package com.muneer.service needs to be intercepted. Also, @Before specifies, that logging needs to happen once the control enters into the method & @After specifies, that logging needs to happen once the control exits the method.
Advice is nothing but, what are the action that needs to be done when a Pointcut is met.
From the code snippet, Line no. 27 & 33 in AspectConfiguration,
logger.info("Started Executing " + joinPoint.getSignature().getName());
So here the Advice is, to start logging the information along with the method name when a Pointcut is met.
At run time, the point at which, when the conditions are met i.e. when the Pointcut is met & advice is being executed, it is referred to as JoinPoints.
It is not limited to only execution of methods, but also when an exception is thrown during Exception Handling Management.
Weaving is nothing but when a Pointcut is met, the corresponding method will get executed.
From the code snippet, Line no. 24 & 25 – When Pointcut is met, it makes sure to execute before() method. This process of binding the Pointcut with the method is referred to as Weaving.
Advantages of Spring AOP
Some of the advantages of Spring AOP are,
- AOP is one of the key components of the Spring Framework. It is important to note that Spring IoC Containers is not dependent on AOP. So, it provides the advantage to developers to whether to use AOP or not.
- As an implementation of cross-cutting concerns, functionalities such as Logging, Notification Management, Authentication, Security, Transaction Management can be kept in a centralized manner & can be used across multiple places in the application.
- As AOP is implemented using Java, there is no need for any special compilation unit or class loaders.
- As the cross-cutting concerns are centralized, Boilerplate coding is reduced.
- It becomes easy for the developers to maintain the system.
- Spring AOP provides XML based configuration as well as advanced Java Annotation configuration.
Examples of Spring AOP
Let us look at a real-time example with additional features such as printing the method parameters & return value with Spring AOP.
From this article, we have done a deep dive into the problem statements before AOP & how Spring AOP solved it efficiently. Also, we have gone through the Spring AOP terminologies, its advantages along with real-time examples & code snippets.
This is a guide to Spring AOP. Here we discuss the introduction to Spring AOP along with its terminologies, advantages, and respective examples. You can also go through our other related articles to learn more–