Spring Tutorial: AOP (Aspect Oriented Programming)

Aspect-oriented programming (AOP) is a programming approach that allows global properties of a program to determine how it is compiled into an executable program. AOP compliments OOPs in the sense that it also provides modularity. But here, the key unit of modularity is an aspect rather than a class. AOP breaks down the logic of program into distinct parts called concerns. This increases modularity by cross-cutting concerns.

A cross-cutting concern is a concern that affects the whole application and is centralized in one location in code like transaction management, authentication, logging, security etc. See the following diagram to understand it better.

AOP can also be considered as a dynamic decorator design pattern. The decorator pattern allows additional behavior to be added to an existing class by wrapping the original class and duplicating its interface and then delegating to the original. You can say that Spring AOP hijacks the executing method, and adds extra functionality before or after the method execution.

Core AOP Concepts

There are mainly seven core concepts in Aspect Oriented Programming. They are depicted via the following diagram:

Let's discuss them in detail.

  1. Aspect : An aspect is a class that implements the JEE application concerns which cuts through multiple classes, such as transaction management, security etc. Aspects can be a normal class configured through Spring XML configuration. It can also be regular classes annotated using @Aspect annotation.
  2. Joinpoint : A joinpoint is a candidate point in the program execution of an application where an aspect can be plugged in. This point could be a method being called, an exception being thrown, or even a field being modified.
  3. Advice: Advices are the actual actions taken for a particular joinpoint. Basically, they are the methods that get executed when a certain joinpoint meets a matching pointcut in the application.
  4. Pointcut: Pointcut are expressions that are matched with join points to determine whether advice needs to be executed or not.
  5. Target Object: They are the object on which advices are applied. In Spring AOP, a subclass is created at runtime where the target method is overridden and advices are included based on their configuration.
  6. Proxy: A proxy is an object that is created after applying advice to the target object. In terms of client, the objects, the target object and the proxy object are same.
  7. Weaving: Weaving is the process of linking an aspect with other application types or objects to create an advised object.

    AOP Advice Types
    • Before: These types of advices execute before the joinpoint methods. They are configured using@Before annotation mark.
    • After returning: These types of advices execute after the joinpoint methods complete executing normally. They are configured using @AfterReturning annotation mark.
    • After throwing: These types of advices execute only when joinpoint method exits by throwing an exception. They are configured using @AfterThrowing annotation mark.
    • After (finally): These types of advices execute after a joinpoint method executes, regardless of the method's exit (whether normal or exceptional return). They are configured using @After annotation mark.
    • Around: These types of advices execute before and after a join point and are configured using @Around annotation mark.