Aspect Orientation

Aspect Orientation

In this tutorial, we will discuss Aspect Orientation in the spring framework. Aspect oriented programming(AOP), as the name suggests, uses aspects in programming. It can be defined as breaking code into different modules, also known as modularisation, where the aspect is the key unit of modularity.

Aspects enable the implementation of crosscutting concerns such as transaction, logging not central to business logic without cluttering the code core to its functionality. It does so by adding additional behavior that is the advice to the existing code.

For example, Security is a crosscutting concern. In many methods in an application, security rules can be applied, therefore repeating the code at every method, defining the functionality in a common class, and controlling where to apply that functionality in the whole application.

In general, in enterprise applications development, if we use Object Oriented Programming languages, we have to combine the Application’s business logic and services logic.

public class Transaction{
   public void deposit(---){
      ----Deposit Logic-----
      ----Authentocation-----
      ----Logging------------
      ----Transact-----------
   }
   
   public void withdraw(---){
      ----Deposit Logic-----
      ----Authentication-----
      ----Logging------------
      ----Transact-----------
   }

   public void transfer(---){
      ----Deposit Logic-----
      ----Authentication-----
      ----Logging------------
      ----Transact-----------
   }
}

If we use the above style of implementation, then we can get the following problems.

  1. It is very difficult to modify the services logic, and it is required to modify in all business methods.
  2. It will not provide Sharability
  3. It will not provide Code Reusability.
  4. It will provide a tightly coupled design.

To overcome the above problems, we have to use Aspect Orientation. Aspect Orientation is not a programming language. It is a methodology or a paradigm. It will be applied to Object-Oriented Programming to get loosely coupled design and improve sharability and Reusability.

The basic idea behind Aspect Orientation is to separate all services logic from System Business logic, declaring every service as an aspect and injecting these aspects into the application Business logic in the respective locations by using Dependency Injection.

Aspect Orientation

In enterprise Applications, AOP will provide the following advantages.

  1. In Enterprise applications, Business components look very clean and have only business implementation statements.
  2. All Services are implemented in a commonplace that simplifies code maintenance.
  3. We can make changes in a common location to reflect the changes in all business methods.

The following vendors implement AOP.

  • AspectJ: It is an extension for Java programming created at the PARC research center. It used Java-like syntax and included IDE integrations for displaying crosscutting structures. It has its own compiler and weaver, on using it enables the use of the whole AspectJ language.
  • JBOSS AOP: It is an open-source Java application server developed by JBoss, used for Java development.
  • Spring AOP: It uses XML-based configuration for implementing AOP, also it uses annotations interpreted by using a library supplied by AspectJ for parsing and matching.

Spring Framework is providing the following two types of implementations

  1. Schema Based Implementation
  2. AspectJ
  • Declarative Based Approach
  • Annotation Based Approach
Introduction to Spring AOP

AOP (Aspect Oriented Programming) introduces a new way of visualizing the programming structure. Like, OOP 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 to implement cross-cutting concerns, i.e., a module or functionality defined in one place but needed in many areas across the project. In simple terms, the cross-cutting concern is something that is centralized in one place of the project & used across multiple areas such as Logging, Authentication, Security, Transaction Management, etc.

Need for Spring AOP

To understand the need for Spring AOP better, let’s look at a problem statement in brief & also how Spring AOP resolved it.

Problem Statement

Let’s say we have around five methods in a service layer & we need to perform some notification when a method is called and when the control exits the method.

We need to call the notification methods during the start & end in all the five methods in the service layer to achieve this.

So, this is a tedious & repetitive work of writing the same code again & again in all five methods. Also, in the future, if we want to remove the notification event for one of the methods, we need to remove the code again. So, there will be a problem with maintenance too.

package com.ashok.spring.aop;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class NotificationService {
 public void startService() {
  // Business Logic
 }
 
 public void stopService() {
  // Business Logic
 }
}
package com.ashok.spring.aop;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class TransactionService {
 private NotificationService notificationService = new NotificationService();
 
 public void loggingService() {
  notificationService.startService();
  
  // Business Logic
  
  notificationService.stopService();
 }
 
 public void emailService() {
  notificationService.startService();
  
  // Business Logic
  
  notificationService.stopService();
 }
 
 public void messagingService() {
  notificationService.startService();
  
  // Business Logic
  
  notificationService.stopService();
 }
 
 public void whatsAppService() {
  notificationService.startService();
  
  // Business Logic
  
  notificationService.stopService();
 }
}

So, if you notice in TransactionService class, for each & every method, we are calling startService() and endService() method from NotificationService. This acts as boilerplate coding & repetitive work for the developers. Also, if the client requires to stop calling the NotificationService for emailService() and loggingService(), we need to remove the code from TransactionService class.

Solution

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.

Aspect Orientation

Scroll to top