java development company
App Development Java Spring

Spring – Aspect Oriented Programming (AOP) | Java Development Company

In Spring, the concept of Aspect Oriented Programming (AOP) is similar to modularity in Object Oriented Programming (OOP). In OOP’s, a key unit of modularity is the object, but in AOP it is aspect.

In an application some of the aspects are scattered (more than one module) which are called as cross-cutting concerns eg. transaction management, logging, security, exception handling, etc… while programming, these cross-cutting concerns will be in a tangled and scattered manner. Without using the concept of Aspect Oriented Programming (AOP)  the same code will be duplicated in different modules. In fact, Spring AOP applied to such modules solves duplication by providing a way to improve modularity by allowing encapsulation of such cross-cutting concerns. At Perfomatix, our dedicated and talented team of expert Java consultants with their comprehensive technical expertise and domain knowledge, continuously focuses on implementing the best practices in Java software development services for cross-platform applications  for deploying the best in class business solutions for our clients.

What is Spring AOP?

Spring Framework enables Aspect Oriented Programming (AOP) by breaking down program logic into distinct parts called concerns. AOP is similar to modularity in Object-Oriented Programming (OOP) which enables the modularization of concerns like security, logging, transaction management, etc…

AOP is a programming technique based on the concept of aspect. Aspect encapsulates cross-cutting concerns which can be defined in one place and functionality can be used in multiple modules by applying a proxy design. It provides a pluggable way to dynamically add cross-cutting concerns before, after or around the actual logic by using simple pluggable configurations. Adding and removing the concerns can be done without recompiling the complete source code which makes easy to maintain code in the present and future, by changing the configuration files. The key benefits of AOP are,

  • Code for an aspect can be defined in a single class which promotes code reuse and easier to change.
  • AOP is applied to business functionality which reduces complexity.
  • No need to make changes to the main application code which makes it configurable.

AOP Core Concepts

  • Aspect: Aspect is a normal class that is configured through Spring XML configuration and implements java enterprise application, that concerns which cut through multiple class like transaction management, security, etc… through spring XML configuration.
  • Advice: Advice is an actual action taken place for a particular joint point. Basically, they are the methods that get executed when a certain joint point needs a matching pointcut in the application. There are different types of advices:
    • Before Advice: It is adviced to execute before a join point.
    • After Advice: It is adviced to execute after a join point.
    • After returning Advice: It is adviced to execute after a join point execution completes.
    • After Throwing: It is adviced to execute after a method execution only if the method exits by throwing an exception.
  • Joint Point: It is a candidate point in the program where an aspect can be plugged-in which defines the method execution, exception handling, etc……
  • Pointcut: It is the expression language of AOP that matches the joint point to determine whether advice needs to be executed/not.
  • Target Object: It is an object on which advices are applied.
  • Proxy: It is an Object that is created after applying advice to target Object.
  • Weaving: It is a process of linking aspects with other application types or objects to create an advised object.

AOP Implementation

Why AOP?

Scenario: For instance, a class has 10  methods that maintain logs and send notifications after calling methods that start from a.

class A{
   public void a1(){...}
   public void a2(){...}
   public void a3(){...}
   public void a4(){...}
   public void a5(){...}
   public void b1(){...}
   public void b2(){...}
   public void c1(){...}
   public void c2(){...}
   public void c3(){...}

Solution without AOP: For maintaining log and sending the notification, the code must be written in all methods. But, in the future, if the notification need not be sent, we have to change all the methods which cause maintenance problem.

Solution with AOP: By using the concept of AOP, we can define the additional concerns like maintaining a log, sending a notification, etc… in the method of a class, thus a method need not be called from another method. It is an entry is given in the XML file / dynamic proxies. So, In the future, if the notification need not be sent, only XML file change / dynamic proxies are required,  which makes the maintenance easy.

Real-time Scenario:For Instance, there is a code for Data Access Object (DAO) which is used to save a user object.

public void saveUser(User user,String userId){
   // add log for this DAO method
   Session currentSession=sessionFactory.getCurrentSession();;

For maintaining log the code must be written in all DAO methods. By using AOP only XML file change / dynamic proxies are required, which makes the maintenance easy. AOP encapsulates cross-cutting concerns which can be defined in one place and functionality can be used in multiple modules by applying a proxy design.


In real time scenarios, Spring AOP is generally used to perform the following,

  1. Transaction Management: The transaction log feature for the entire application can be handled in just one Aspect class by using AOP, instead of writing the transaction log in every module. 
  2. Logging: By using AOP, the log feature that is used in every module can be handled by using just one Aspect class that which helps for maintaining the log of the entire application.
  3. Performance Monitor: For monitoring the execution time of any method AOP helps to calculate start time and end time by adding that simple logic in Aspect class.
  4. Exception handling: Exception Handling with AOP can reduce the lines of code for exception handling, thus improving code maintenance and agility of the application by notifying the DevOps team via SMS/email.
  5. Security: AOP provides security mechanisms by weaving a security aspect in an application. Implementation of security with AOP is a flexible method to develop separated, extensible and reusable pieces of code.
  6. Audit Log Management: Audit log actions can be tracked effectively by using AOP (e.g. who, what, when, where).
  7. API  By using AOP, API analytics can be done for evaluating the performance. (such as what is the peak time? / what is the average load? / how many times a method has been called  by the user?).


 As mentioned above, AOP helps is developing loosely coupled applications by implementing the cross-cutting concerns of the application that separates from the business logic. For example, by implementing AOP in Login Module can handle log feature using just one Aspect class, that will put all required log for the entire application and no need to write log in every method.

Need Help? Java Development Company

Perfomatix is a top Java Development company with certified Java resources available for offshore software development. Top app development companies can guide you to upgrade your business right now. 

Check out our expertise to know more.

Fast Forward Innovation with Perfomatix. 

Talk to our experts now and let us be your innovation partner!

Perfomatix | Product Engineering Services Company