AOP In Spring


AOP stands for Aspect-Oriented Programming, which is a concept used in the Spring framework with a target to increase modularity by using cross-cutting concerns. Here, in this concept, the key unit of modularity is aspect instead of class. AOP just increases the modularity by breaking the program logic into separate parts (these are also called concerns). A cross-cutting concern is a type of concern that can alter the entire application. So these concerns should be placed in the center of the programming code as much as possible. These concerns can be like transaction management, logging, data validation, security, etc; that need to be cut across multiple types and objects.

AOP in Spring is used to add extra concerns in the existing code without modifying the code itself to achieve modularity. We can maintain this code anytime in the future without affecting the main logic. Adding and removing the concerns can be achieved without compiling the entire source code again. It can be done by changing configuration files (in the case of aspects using XML configuration).

Contrary to the OOP (Object Oriented Programming), where modularity is achieved by Classes, In AOP, it is achieved by aspects that are configured to cut across different classes. Spring AOP derives direct dependency of crosscutting concerns from classes while in normal object-oriented programming, it is not possible.

Scenarios of using AOP?

  • AOP is used in the Spring framework to provide declarative enterprise services such as declarative transaction management as a substitute for EJB declarative services.
  • It enables users to implement custom aspects that encourage the user to use AOP, instead of OOP.

Why choose AOP?

In the above explanation, we hope you get a glimpse of the AOP concept, with its basic function of providing a pluggable way for adding additional concern before, after, or around the actual logic dynamically. Now, we will discuss some points that make an AOP the preferred choice.

Code Reuse Quickly

The AOP provides you with the functionality of using the code again in a quick time. It lets the programmer reuse code across multiple classes without altering the pre-existing code. It provides an annotation named "Logged" that enables us to modify a number of classes without repeating exact logging logic.

Third-Party Code Dealing

If we want to inject shared behavior into a function that we use in our core components, then this code can be proved by a third-party library or framework. After that, we can’t alter that code even if it is from open source. But with AOP, you can do this without altering the third-party code at all.

Cross-Cutting Concerns

You would probably know the term Cross-Cutting Concerns, as we mentioned many times earlier in this tutorial. It is the main functionality of the AOP. As a Centrepoint of the AOP, it lets a programmer suddenly break the core program logic into several tasks or concerns. In this process, the interesting fact is that the main behavior of the core components like authentication, logging, tracing, error handling, etc; is unchanged. In fact, your core components turn out to be more readable and alterable as a result.

Now, let’s Better Understand the Concept of AOP with a Simple Example:

Suppose there is a class named A that contains ten methods:

class A{  
public void m1(){...}  
public void m2(){...}  
public void m3(){...}  
public void m4(){...}  
public void m5(){...}  
public void n1(){...}  
public void n2(){...}  
public void p1(){...}  
public void p2(){...}  
public void p3(){...}  

In the class above, there are five methods ranging from m1……...to m5, similarly, 2 methods start from n and 3 methods start from p.

Now the question is how can I maintain a log and send a notification after calling methods that start from m?

In this scenario, we can call methods that maintain a log and send notification) from the methods starting with m without using AOP. But in this case, we have to write the code in all five methods.

But if your client changes their mind, and indicates that he/she doesn't have to send a notification, you need to change all the methods. It results in the occurrence of maintenance problems.

Now with the AOP, there comes a solution that eliminates the need for calling methods from the method. We can define the extra concerns such as maintaining a log, sending notifications, etc. in the method of a class. The XML file will store its entry.

If your client wants to remove the notifier functionality, it can be done by altering the XML file only, making the log and notification maintenance easy and comprehensive.

AOP Concepts and Terminology

Now we will study the core concepts and terminology of Aspect-Oriented Programming before going further into the implementation of Spring AOP.

Join Point

A Join point can be any of these programs like method execution, exception handling, field access, etc. But only method execution is supported by the Spring. It is a point in the program where an aspect can be plugged in.


Advice is an action of aspect. The aspect takes this action at a particular join point. In the context of programming, when a certain join point with a matching pointcut is accomplished in the application, these methods are executed. Struts2 interceptors or Servlet Filters are some of the examples of Advice. In Spring, They are termed as interceptors that maintain the order of interceptors around the join point.

There are five types of Advice in Spring AOP:

  1. Before Advice: it executes before a join point. It cannot prevent the flow of execution that proceeds to the join point (unless it throws an exception)
  2. After Returning Advice: it executes after a join point completes in a normal manner.
  3. After Throwing Advice: it executes in the case of a method exiting by throwing an exception.
  4. After (finally) Advice: it executes after a join point. No matter whether the join point exits normally or has an exceptional return.
  5. Around Advice: This component of AOP is executed before and after a join point. It is the most powerful type of advice. It is also able to choose the option of proceeding to the join point, make a shortcut of the advised method execution by returning its own value or throwing an exception.


A Pointcut is used as an expression language of AOP that matches a particular join point. This expression can run at any Joinpoint matched by the Pointcut. A Pointcut decides whether the Advice needs to be executed or not. They use a variety of expressions that match the join points. Spring uses AspectJ pointcut expression language. @Around is the most powerful example of Pointcut. They can select one or more Join Points.


It represents additional methods and fields for a particular type. It will represent a new interface to any of the advised objects. It can be used to allow a bean to implement an IsModified interface to make caching simple. It is also called an inter-type declaration in the AspectJ community.

Target Object

A target object is one that is advised by one or more aspects. In Spring AOP, the alternate of the target object is the proxied object because it is implemented by runtime proxies. In the AOP concept of Spring, a subclass is created at runtime which overrides the target method and includes advice components on their configuration basis. A target object is also termed the advised object.


A class that includes advice, join points, etc. is called an Aspect. In this class, a concern that cuts across multiple classes is modularized to simplify the programming. The example that comes in this category is Unified logging. An Aspect is a new logic that can be added to the existing class, method, sets of classes, or sets of methods. It is mostly used in JEE applications. In Spring AOP, the Aspect component gets executed by using regular classes or regular classes with annotated expressions such as @Aspect.


It is a type of aspect including one advice only.

AOP Proxy

It is responsible for implementing aspect contracts, created by the AOP framework. It can be either a JDK dynamic proxy or a CGLIB proxy in the spring framework. AOP Proxy is used to create the Proxy classes that contain target classes and advice invocations.


In this core concept of Spring AOP, linking aspects with other application types or objects are performed to create an advised object. It is a procedure of combining the aspects with the main code. This process can be accomplished at compile time, load time, or runtime. But, in Spring AOP, it is performed at runtime.

AOP Implementations

Spring AOP can be implemented in the three ways:

  • AspectJ
  • Spring AOP
  • JBoss AOP

Spring AspectJ Annotation Style is the most preferred type to implement AOP.