Pages

Saturday, August 4, 2012

Aspect Oriented Programming with spring

In software programming, we meet cross cutting concerns in out app. Security, transaction handling, logging are examples to some of those. So Aspect oriented programming focuses on handling these concerns.
First of all why is it not okay to use a separate class or something to handle these concerns? For example why not logging with a class?

Well there are mainly 3 disadvantages 

1) We have to call methods using references to the class we created. - In logging we will be creating a logging object and calling a method within it when we need to use logging. Now say you modify or remove the logger class, then you will have change a lot of code in your program where you called the methods in the logger class. So it's a lot of mess for one little thing.

2) Too many relationships to the cross cutting concerns - You will have too many relationships to the logger class by referencing it from other classes. In the case of logger, it will be having too many relations from other classes. We do not want that. Simply because logger does not add a lot of business value to the program but only a helper.

3) cannot change all at once. - you will have to change codes in each and every class.

So how are these concerned solved?

Well we handle these by creating aspects (For the moment let's say aspect is a class with special privileges) like and aspect for logging, another for security, and another for Transactions etc...
Now how do we apply these aspects to the code? We do not create objects and call methods from other classes where the aspect is needed. Instead, an aspect configuration is created.
Now the question is how does the configuration handle it? well the configuration contains where to which classes and methods in the program the aspect should be acted upon. Therefore in a way, the aspect configuration is something that glues the aspects and the business logic objects together.


Eg: Say you need to call a security method in classA and classB. What you do is first create the security aspect and implement the methods. Then create classA and classB. Then create the aspect configuration. The configuration says to which methods in classA and classB, the methods in security aspects needs to be called for. It can specifically say which methods in security to call for a certain method in classA , which methods to call for a certain method in classB. aspects can be applied before or/and after the methods.

So if you want to do a bulk change, you can just chang the aspect configuration instead of changing everywhere.

No comments:

Post a Comment