Spring AOP – Aspect Oriented Programming

AOP is like triggers in programming languages such as Perl, .NET, Java, and others.
Spring AOP module provides interceptors to intercept an application. For example, when a method is executed, you can add extra functionality which executes always before or after the method execution using annotations and xml configurations.
AOP involves breaking down program logic into distinct parts called concerns. Aspects enable the modularization of concerns that cut across multiple types and objects (often termed crosscutting concerns).
Common examples are – logging, auditing, transactions, security, caching, etc.

Given a scenario :

You have to maintain log and send notifications when methods starting with *serviceImpl are called.
One way : is to write the log statements in every serviceImpl methods
Problem : after you write the logs, if requirement comes to change the logging statment, then you need to change it in every possible method where you wrote the log statement, it leads to severe maintenance problem.
Better way : Define a concerns and use it. Even if you want to change the logs then you need to change it at only one place and not in every file.

AOP terminologies

1. Aspect : An Aspect is the concern (cross cutting concern) which you want to implement in the application such as logging, performance monitoring, transactional handing etc.

2. Advice : An Advice is the actual implementation of the aspect. Aspect is a concept and Advice is the concrete implementation of the concept.

3. Join Point : JoinPoint is a point in the execution of the program where an aspect can be applied. It could be before/after executing the method, before throwing an exception, before/after modifying an instance variable etc. Keep in mind that it is not necessary and also not required to apply an aspect at all the available join points. Spring AOP only supports method execution join points.

4. Aspect : An Aspect is the concern (cross cutting concern) which you want to implement in the application such as logging, performance monitoring, transactional handing etc.

5. Point cut : PointCuts tell on which join points the aspect will be applied. An advice is associated with a point cut expression and is applied to a join point which matches the point cut expression.

6. Target : An Aspect is the concern (cross cutting concern) which you want to implement in the application such as logging, performance monitoring, transactional handing etc.

7. Weaving : Weaving is the process of linking aspects with other application types or objects to create an advised object. This can be done at compile time, load time, or at runtime.

Types of Advice Spring aspects work with these advice

  • Before Advice: it executes before a join point.
  • After Returning Advice: it executes after a joint point completes normally.
  • After Throwing Advice: it executes if method exits by throwing an exception.
  • After (finally) Advice: it executes after a join point regardless of join point exit whether normally or exceptional return.
  • Around Advice: It executes before and after a join point.

Summary

In this tutorial we learnt about Spring Aspect Oriented Programming, AOP terminologies, a scenario where it can be used and types of Advice.
Hope you liked it !


Leave a Comment