Intro to Spring AOP

Reading Time: 2 minutes

The world is a stage where all of us are artists and Constant learning is the foundation of success.
In order to continue your learning with something new, here were are going to learn about working with Spring AOP.
Here we will look at what AOP is, and what features we have with AOP.

Let’s start with the intro first.

Spring AOP

AOP stands for Aspect-Oriented Programming
Spring AOP is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. AOP complements OOPs in the sense that it also provides modularity.
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.

Cross-Cutting Concerns

A concern is a part of a system divided on the basis of functionality. It can be as general as the details of the database interaction or as specific as performing a primitive calculation.
Cross-cutting concerns are shared across multiple application modules. They represent functionalities for secondary requirements. for example logging, security, and data transfer are concerns that are needed in almost every module of an application

Maven Dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
</dependencies>

AOP Concepts and Terminology

  • Joinpoint
  • Pointcut
  • Advice
  • Aspect
  • AOP Proxy
  • Weaving

Joinpoint

Joinpoint is a point during the execution of a program, such as the execution of a method or the handling of an exception.

Pointcut

It is an expression language of AOP that matches join points.

Advice

Advice is an action taken by an aspect at a particular Joinpoint. Different types of advice include “around,” “before,” and “after.”

Aspect

An aspect is the modularization of a concern that cuts across multiple classes. Unified logging can be an example of such a cross-cutting concern.

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 from proceeding to the joinpoint (unless it throws an exception).

After-Returning Advice

Advice to be executed after a join point completes normally

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

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

Why use AOP

It provides the pluggable way to dynamically add the additional concern before, after, or around the actual logic.

When to use AOP

  • When we have to provide declarative enterprise services such as declarative transaction management.
  • It allows users to implement custom aspects.

That’s pretty much it from the article. If you have any feedback or queries, please do let me know in the comments. Also, if you liked the article, please give me a thumbs up and I will keep writing blogs like this for you in the future as well. Keep reading and Keep coding.

Reference

Written by 

I am a genius middle-class noble philanthropist(by heart) . & Simply a technology enthusiast who loves to share and gain knowledge through blogs.