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

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
  2. The healthcare industry is one of the prominent and busiest sectors. The usage of sign boards for hospitals helps both patients and healthcare workers. Digital sign boards in hospitals provide information about the latest trends in the healthcare industry and health tips for patients. Many hospitals use sign boards inside as well as outside their premises. If it is used inside the hospitals, it displays information about the patients, and the next person waiting in the queue and also provides directions. Many manufacturers provide sign boards for hospitals and hospitals must choose the best provider.

    ReplyDelete

Post a Comment

Popular posts from this blog

Pivotal Cloud Foundry (PCF) Integration with Elastic Cloud Storage (ECS)

Restful code example using Spring MVC

Spring Integration - Bulk processing Example