Thursday, September 26, 2013

Spring AOP - Part I

In this blog I'm going to write about AOP using Spring. 

Overview:

AOP which stands for Aspect Oriented Programming aims at separating out cross cutting concerns from the normal programming logic. This makes way for cleaner application code can focus on business logic and the other concerns like logging, security etc will be handled by the aspects.


Illustration:

To give a perspective of how AOP works, let us consider the following example:

Let us say, in a big city, there are no proper sign boards. So the city Mayor decides to take up a project to install sign boards. This project is like a cross cutting concern in some way. 

The city corporation will define certain rules to install the sign boards. 
Sign board rules:
  1. A sign board indicating 'school nearby' should be placed 5 meters from the school in both directions. 
  2. A sign board indicating a sharp curve should be placed 5 meters before the curve. 
  3. A sign board 'Thank you. Visit again' should be placed after the city exit.
  4. A sign board 'Welcome to the city' should be placed before entering the city.

Terminologies:

Now, let us try to correlate the above example with AOP. There are certain terminologies which are defined by AOP.
  1. Aspect: Module which defines the cross cutting concern.
  2. Advice: Defines the task to be done by the cross cutting concern. In the above example, it is the display contents of the sign board.
  3. Join Point: Defines the exact places where the advice should be applied. Spring AOP supports only method level advices.
  4. Point cut: An expression which identifies the Join Point. In our example, this could be the identifying places like school, city exit etc.
  5. Introduction: Allows you to declare additional methods or fields.
  6. Target Object: The object which is being adviced.
  7. Weaving: The actual process of applying the advice. Spring AOP applies advice at run time using Dynamic Proxies (Refer this blog for more details on Proxy pattern).
Spring supports 5 types of advices:
  1. Before advice - The Advice executes before the specified join point. In the above eg, it is the sign board which is placed before the  sharp curve.
  2. After returning advice - The advice executes after the specified join point. (This happens only if the method did not throw any exception). In the above eg, it is the sign board after the city exit.
  3. After throwing advice - The advice executes when the method throws exception.
  4. After advice - The advice executes regardless of exception being thrown or not.
  5. Around advice - The advice surrounds the join point. It has provisions to execute some piece of code before and after the method execution. You can also chose whether to proceed with the method invocation or not. In the above eg, it is the sign board while entering the city and the city exit.

                                                                             Click here to continue....

2 comments:

  1. what is cross cutting concerns ?

    ReplyDelete
    Replies
    1. Cross cutting concern is anything which will apply to different layers of the application and is not necessarily concerned with functional aspects of the application. For eg., Security, transactions are all treated as cross cutting concerns.

      Delete