Spring AOP Tutorial – When & Where use Aspect Oriented Programming

Keeping you updated with latest technology trends, Join DataFlair on Telegram

1. Objective

In our previous tutorial, Spring Annotation Based Configuration. Here, in this Spring AOP Tutorial, you will see about the Aspect-Oriented Programming Moreover, we will discuss why and where we use Spring Framework AOP. At last, we will cover the Spring AOP Terms, types of Advice, and example.

So, let’s start the Spring Aspect Oriented Programming Tutorial.


2. What is Spring AOP (Aspect Oriented Programming)?

Aspect-oriented programming (AOP) is one of the major components of the Spring Framework. The Spring AOP helps in breaking down the logic of the program into several distinct parts called as concerns. Cross-cutting concerns is the functions which span multiple points of an application.

The cross-cutting concerns help in increasing the modularity and separate it from the business logic of an application. Also, a cross-cutting is a concern that affects the whole application and it should be centralized in one location in code as possible such as authentication, transaction management, logging etc.

Let’s see what is Spring Bean Definition – Spring Configuration Metadata

We can implement Spring Framework AOP in pure Java, it doesn’t require to the special compilation process. It is the best choice for use in a J2EE web container or application server because it doesn’t require to control the class loader hierarchy.

a. Need for Spring AOP

The Spring AOP provides the pluggable way of dynamically adding the additional concern before, after or around the actual logic.

Consider there are 10 methods in the class as defined below:

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(){...}  

These are the 5 methods that start from m while 2 methods start from n and 3 methods starting from p.

  • The Scenario: You have to maintain the log and send the notification after calling methods starting from m.
  • A problem without using the AOP: You can call methods which maintain logs and sends the notification from the methods starting with m. For that, you need to write code for all the 5 methods.
  • The solution with Aspect Oriented Programming: You don’t have to call methods from the method. You can define additional concerns like maintaining a log, sending notification etc. as a method of a class.

b. Where to use Spring Aspect Oriented Programming

Some of the cases where AOP is frequently used: To provide declarative enterprise services. For example, as declarative transaction management. It allows users for implementing custom aspects.

Let’s explore Spring Batch Tutorial

3. Terminologies in Aspect Oriented Programming

Before starting the work with Spring Aspect Oriented Programming you should become familiar with the concepts of Aspect-oriented programming and its terminologies. Some of the terms are not Spring-specific but they are related to AOP.

Spring AOP Tutorial - Terminologies

Spring Framework Aspect Oriented Programming – Terminology

i. Aspect

This module has a set of APIs providing cross-cutting requirements. Like for example, a logging module would be calling AOP aspect for logging. The application can have any number of aspects depending on the requirement.

ii. Join Point

This represents the point the application where the Spring AOP aspect can be plugged-in. In another way, you can say that it is the actual place in your application where the action will be taken using the Spring AOP Framework.

Read about Spring Bean Definition Inheritance & Bean Definition Template

iii. Advice

This is the action taken either before or after the execution of the method. This is an actual piece of code that gets invoked during the execution of the program by the Spring AOP Framework.

Spring AOP Tutorial - Terminologies

AOP Terminologies in Spring Framework

iv. Pointcut

It can be understood as an expression language of Spring AOP that join points.

v. Introduction

It means the introduction of additional methods and fields for a type. Also, it allows you to introduce a new interface to any advised object.

vi. Target Object

The object advises by one or more aspects and it is also known as the proxied object in the Spring Framework as Spring AOP implement using the runtime proxies.

vii. Weaving

Waving is consider as the process of linking the aspect with other application types or objects to create an advised object. Weaving done at the time of compilation or during the load time or runtime. Spring AOP performs weaving during runtime.

viii. AOP Proxy

AOP Proxy use for implementing the aspect contracts which create by AOP Framework. It will be a JDK dynamic proxy in Spring Framework.

Do you know about Spring Dependency Injection – Types of Spring DI & Example

ix. Interceptor

It is an aspect which contains advice, joinpoints etc.

4. Types of Advice in Spring AOP

Spring Aspects can work with five kinds of advice as defined below:

Spring AOP Tutorial - Terminologies

Types of Advice in AOP

  • Before: Run the advice before the execution of the method.
  • After: Run the advice after the execution of a method regardless of the outcome.
  • After-returning: Run the advice after the execution of the method only after the successful completion of the method.
  • After-throwing: Run the advice after execution of method only if the method exits throwing an exception.
  • Around: Run the advice before and after advice method gets invoked.

5. Summary of Spring AOP Tutorial

Hence, in this Spring AOP Tutorial, you learned about the Aspect-oriented programming in Spring Framework. You also saw the need for using Spring AOP, where to use it. Along with that, you saw its terminologies and the types of advice. Still, you had a query, feel free to ask in the comment section.

Related Topic- Spring MVC Tiles 3 Integration

For Reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.