Spring AOP Tutorial

In this Page, you will learn :
  • What is AOP?
  • Where use AOP?
  • How can Implementation Spring AOP in project ?

Aspect Oriented Programming (AOP) compliments OOPs as in it additionally gives particularity . However, the key unit of particularity is angle than class.

AOP breaks the system rationale into particular parts (called concerns). It is utilized to build particularity by cross-cutting concerns.

A cross-cutting concern is worry that can influence the entire application and ought to be unified in one area in code as would be prudent, for example, exchange administration, verification, logging, transaction management, authentication, logging, security and so on.


Why use AOP?

It gives the pluggable approach to rapidly add the extra concern some time recently, after or around the real rationale. Assume there are 10 routines in a class as given beneath:

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

There are 5 systems that begins from m, 2 routines that begins from n and 3 strategies that begins from p.

Understanding Scenario I need to keep up log and send warning in the wake of calling routines that begins from m.

Issue without AOP We can call systems (that keeps up log and sends notice) from the routines beginning with m. In such situation, we have to compose the code in all the 5 techniques.

Solution with AOP We don't have to call methods from the method. Now we can define the additional concern like maintaining log, sending notification etc. in the method of a class. Its entry is given in the xml file.

In future, if client says to remove the notifier functionality, we need to change only in the xml file. So, maintenance is easy in AOP.


Where to use AOP?

AOP is mostly used in following cases:

  • to provide declarative enterprise services such as declarative transaction management.
  • It allows users to implement custom aspects.
  • Logging-related.
  • Security checks.
  • Tweaking of a legacy application

AOP Concepts and Terminology

AOP concepts and terminologies are as follows:

  • Join point
  • Advice
  • Pointcut
  • Introduction
  • Target Object
  • Aspect
  • Interceptor
  • AOP Proxy
  • Weaving

Join point

Join point is any point in your program such as method execution, exception handling, field access etc. Spring supports only method execution join point.


Advice

Advice represents an action taken by an aspect at a particular join point. There are different types of advices:

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

Pointcut

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


Introduction

It means introduction of additional method and fields for a type. It allows you to introduce new interface to any advised object.


Target Object

It is the object i.e. being advised by one or more aspects. It is also known as proxied object in spring because Spring AOP is implemented using runtime proxies.


Aspect

It is a class that contains advices, joinpoints etc.


Interceptor

It is an aspect that contains only one advice.


AOP Proxy

It is used to implement aspect contracts, created by AOP framework. It will be a JDK dynamic proxy or CGLIB proxy in spring framework.


Weaving

It is the process of linking aspect with other application types or objects to create an advised object. Weaving can be done at compile time, load time or runtime. Spring AOP performs weaving at runtime.


AOP Implementations

AOP implementations are provided by:

  1. AspectJ
  2. Spring AOP
  3. JBoss AOP

Spring AOP

Spring AOP can be used by 3 ways given below. But the widely used approach is Spring AspectJ Annotation Style. The 3 ways to use spring AOP are given Next Page :