Spring Aspect Oriented Programming (AOP)

Thanusri swetha J October 22, 12:10 PM Technology

AOP is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does this by adding additional behavior to existing code without modifying the code itself. Instead, we can declare the new code and the new behaviors separately. Spring's AOP framework helps us implement these cross-cutting concerns. [1]

Figure 1. Spring Aspect oriented programming (AOP)

Figure 1 shows Aspect oriented programming (AOP) as the name suggests uses aspects in programming. It can be defined as the breaking of 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 behaviour that is the advice to the existing code. [2]

Advice, joinpoint or pointcut

  • An important term in AOP is advice. It is the action taken by an aspect at a particular join-point.
  • Joinpoint is a point of execution of the program, such as the execution of a method or the handling of an exception. In Spring AOP, a joinpoint always represents a method execution.
  • Pointcut is a predicate or expression that matches join points.
  • Advice is associated with a pointcut expression and runs at any join point matched by the pointcut.
  • Spring uses the AspectJ pointcut expression language by default.

Types of AOP Advices

There are five types of advice in spring AOP.

  • Before advice: Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).
  • After returning advice: Advice to be executed after a join point completes normally: for example, if a method returns without throwing an exception.
  • After throwing advice: Advice to be executed if a method exits by throwing an exception.
  • After advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).
  • Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception. [3]
References:
  1. https://www.baeldung.com/spring-aop
  2. https://www.geeksforgeeks.org/aspect-oriented-programming-and-aop-in-spring-framework/
  3. https://howtodoinjava.com/spring-aop-tutorial/
Cite this article:

Thanusri swetha J (2021), Spring Aspect Oriented Programming (AOP), Anatechmaz, pp. 66

Recent Post

Blog Archive