Professor Bijou
Professor Bijou The professor is a very mysterious man, but a man who loves everyone he comes across. Except birds, Bijou hates birds. In his spare time away from writing, Bijou likes to go on long drives in the country and ponder his life.

Spring AOP Tutorial - A Complete Tutorial of 2018 Recently

Spring AOP Tutorial - A Complete Tutorial of 2018 Recently

Spring AOP Tutorial - A Complete Tutorial of 2018 Recently

  1. Objective

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

Spring AOP Tutorial - A Complete Tutorial of 2018

  1. Spring Tutorial on AOP

Aspect-oriented programming (AOP) is one of the major components of the Spring Framework. This helps in breaking down the logic of the program into several distinct parts called as concerns. The functions which span multiple points of an application are called as cross-cutting concerns. The cross-cutting concerns help in increasing the modularity and are separated 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.

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 AOP: 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.

  1. Spring AOP Tutorial - Terminologies

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 Framework AOP Terms

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 Aspect Oriented Programming Framework.

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 Aspect Oriented Programming Framework.

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.

ix. Interceptor

It is an aspect which contains advice, joinpoints etc.

  1. Types of Advice in Spring AOP

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

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 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 invoke.

So, this was all about Spring Aspect Oriented Programming Tutorial. Hope you like our explanation.

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. For more articles on Spring Framework, refer to our articles on https://data-flair.training/blogs/category/spring/

Article Source: [http://EzineArticles.com/?Spring-AOP-Tutorial—A-Complete-Tutorial-of-2018-Recently&id=9978664] Spring AOP Tutorial - A Complete Tutorial of 2018 Recently