AOP(Aspect
Oreinted Programing)
AOP is a not just a feature provided by spring but is a model of programming itself. Before we start, Lets look at some other programming standards
Functional Programming
If you have written code in older programming languages like C would know what exactly it is.In this language,you will think of problems and break them into different functions. Each function performs different logic and each function call other function and pass output to other function ,at the end we get the result from the last function.
The problem here is that if the complex design is involved and then you would
have whole lot of functions and whole lot of dependencies.
To over come this OOPs (Object Oriented Programming)is introduced
You would not think of different functions when you are trying to solve a problem by writing code. You will think of individual entity as objects and than you will write object that mirror different entity in your problem space in that way you will have less dependencies and each object is performing task individually without any dependencies
To over come this OOPs (Object Oriented Programming)is introduced
You would not think of different functions when you are trying to solve a problem by writing code. You will think of individual entity as objects and than you will write object that mirror different entity in your problem space in that way you will have less dependencies and each object is performing task individually without any dependencies
Object Oriented
Programming
In this type of programming technique you will have different object A, B and C which would have different properties as member variables and Methods ,so you have encapsulated the entities and you can have more complex code because you have cleaner design and separation of concerns ,This is good but you can not make all your task as object,
Lets take an example,What about common procedure we have a logMessage() method to be run on all these objects .Now what log message do is, it prints out a simple log messages and you have to give this functionality in all those object. So what you do is you call this logMessage () in all the objects you are creating .This is not really a good design because you are repeating your code by calling it in all objects,so how would you refine this code
Separate Object
One way is you can take that message out from all the object and
create another object which will have this method ,ofcourse you will need to
include this method inside an object so you have to call this logger method by
creating object using new or it can be static method or it can be parent class
and you will inherit it to use this method ,this works fine but there are
couple of problems here ,
1. As all are object use Logger object but, this does no help in
our business logic so there is no need to have our object to be dependent on
this logger object because its just printing logs and nothing else.
Problem is that, too many relationships to the crosscutting object
.By Crosscutting i mean object that concerns different object, object in your
problem domain
2. Code is still required to call in all your code()
3. Cannot do all the changes at once suppose you have to change in
bulk in your object it is difficult and you would need to go to each class and
do the changes.
Cross Cutting Concerns
Some functionality that need to be used by different object in
your object and that may not be part of your problem domain(Business logic).It
could be infrastructure related ,security related
Some of them are:
- Logging
- Transaction
- Security
Since there are common problem their are way and so we use AOP
Aspects
In this
case you would not create a new class for the cross cutting concerns but you
create an aspect its actually a class a special class i would say.
we will look how aspect class is different from other class but for now think
as a class with special features.
Some would think how aspect is different from object
If we create an object or aspect the difference is :
when u create different aspect you don't reference these reference in your code If it is a normal class you will create object using new and use it reference to call the method instead of this you will have
Some would think how aspect is different from object
If we create an object or aspect the difference is :
when u create different aspect you don't reference these reference in your code If it is a normal class you will create object using new and use it reference to call the method instead of this you will have
Aspects
Aspect
configuration file which tells to which object and method and which class these
aspect should applied to so this is what different from traditionally class .
So let have a look how this works :
Lets assume we have to performs a function lets say Logging functionality in all the object so what we will do is write the configuration that call logging function before those methods get executed so this is what i tell spring to do and spring will make sure that the logging method is called as before the method for those configurations given
Advantages:
Lets say we have to perform logging in method A and B Object first and don't want to give the same for C method of second object then we can have configuration related to the method of Object first and will the logging called but later on we want to have logging for Second class also then we will change in configuration only and not get to the class and make changes only have to change the configuration and will have the functionality provided to that method of Object
This solve all the problems we have discussed earlier
Lets assume we have to performs a function lets say Logging functionality in all the object so what we will do is write the configuration that call logging function before those methods get executed so this is what i tell spring to do and spring will make sure that the logging method is called as before the method for those configurations given
Advantages:
Lets say we have to perform logging in method A and B Object first and don't want to give the same for C method of second object then we can have configuration related to the method of Object first and will the logging called but later on we want to have logging for Second class also then we will change in configuration only and not get to the class and make changes only have to change the configuration and will have the functionality provided to that method of Object
This solve all the problems we have discussed earlier
- Flexibility
- Different
flow of controls
Wrapping Aspect Around Methods

Steps in Aop
- Write
Aspect
- Configure
where the aspects apply
Thanks for reading.




0 comments :
Post a Comment