Spring AOP tutorials and example codes.


 

Spring AOP tutorials and example codes.

In this tutorial you will learn about the AOP module of spring framework.

In this tutorial you will learn about the AOP module of spring framework.

Spring AOP (Aspect Oriented Programming)

What is Spring AOP (Aspect Oriented Programming)?

Spring AOP is a new programming paradigm, which is implemented in pure java and there is no need of special compilation process. It does not need to control the class loader hierarchy, and is thus suitable for use in a Java EE web container or application server.
It is another way of thinking about program structure in terms of aspects or concern. If we compare AOP with OOP (Object oriented Programming), the OOP decomposes the application into hierarchy of objects where as the AOP decomposes the application into Aspect or Concern.  

AOP Concepts

Aspect

An aspect is a subprogram that is associated with a specific property of a program. As that property varies, the effect "ripples" through the entire program. It is functionality or feature that crosscuts over object. AOP also increases modularity of a program.
An aspect is scattered by virtue of function being read over a numbers of unrelated functions. That means changing in one function requires modifying all affected modules. As code making it harder to understand and maintain.

Advice

An advice is an action taken by the aspect at a particular joint point. It is a chunk of code that is executed during program execution. There are various types of advices depending upon the position they are called in a program, such as before, after, around etc.

Types of Advices

1. Before Advice:- The advice which executed before a joinpoint called before advice. The before advice does not have the ability to interrupt the execution flow proceeding at the joint point unless it throws an exception.

2. After return Advice:- The advice which executed after a jointpoint completed normally, called after advice. For example a method returns without throwing an exception.

3. Around Advice:- Around advice is responsible for choosing whether to proceed to the joinpoint or to shortcut executing by returning their own return value or throwing an exception. It performs the custom behavior before and after method invocation by surrounding a joinpoint.

4. After Throwing Advice:- This advice is executed when a method throws an exception.

5. After (finally) Advice:- The advice is executed when program exits the joinpoints normally or by throwing an exception..

Weaving

Weaving is a process of linking aspect with other application or object to create an advice type. This can be accomplished/performed at compile time, run time load time. In spring framework weaving is performed at run time.

Join point

A join point is a point used in Spring AOP framework to represent a method execution. It always point during execution of program or methods or exceptions. It is actually beginning of methods, AOP intercepts at this point to do some other stuff. i.e. beginning of methods doThis(), doExtra(), test(), somethingElse() etc.

Pointcut

In AOP a pointcut is a set of many joinpoints where an advice can execute. When the program execution is reaches to the one of joint point described in pointcut, a chunk of code (known as Advice) associated with that pointcut executed. This provides the extra flexibility to the programmer to merge an additional code, which is executed in already defined point. It is a key concept of AOP, which distinguishes it from older technology offering interception.
Spring supports union, and intersection operation on pointcuts. Union means the method that either pointcut matches. It is more useful pointcut. And Intersection means the method that both pointcuts match. In the above advices around advice is most general type of advice used in AOP.

AOP Proxy

AOP proxy is an object used to implement the aspect contract. This object is created by the AOP framework. In spring framework AOP proxy is JDK dynamic proxy or CGLIB proxy.

Target Object

Those object in spring framework, which is advised by one or more aspects are called target object. These object is always be proxied object because spring AOP is implemented using runtime proxies.

Ads