Just to have background about what is Aspect Oriented Programming :Well we are in Object Oriented programming world.
Everything is object and concepts of OOP’s can applied very easily.Things are easy to maintain as things are very well encapsulated under objects and hence code changes are easy to implement and maintain.
This happens because each Object has a specific task ,and in well written OOP’s code change in some functionality involves changes at few places or to say only concerned objects change.
This is generally applicable for Business requirements which can be easily modularized,but what about the cross-cutting concerns.
Cross-Cutting Concerns: The concerns which are to be taken care of ,in almost every part of code or in other words,code for these concerns is scattered all around the code base.In short these requirement cross-cut at many places and hence regular concepts of OOP’s are not very useful in handling them.
These are generally requirements not specified by the user , but are needed for over-all well being of the projects.
So what are these kinds of requirements:
- Security: Security checks may be need at each and every class.
- Logging: Logging is a typical example of cross-cutting concerns.
- Profiling:Need to know bottleneck in the system,so typically present at everywhere.
Now as this type of code will scattered everywhere in the main code.Other then reason’s like maintenance etc ,it also reduces the code readability and make code ugly.
We imagine if you have to change something ,then we need to go and change thousand’s of classes,ex:If we decide to change the way logging has been implemented,then probably we will need to open each and every file in the project and do the change.And we all know changes are the main cause of getting infected with new bugs.So again the goal is to minimize the effect or localize the effect of change.
Q)So how these cross-cutting features come’s into play?
A) We define pointCuts:These are basically some conditions which when fulfilled starts the magic.
so a pointcut in annotation will be defined as @Pointcut(“execution(* com.model.service.EmployeeService.*(..))”)
public void employeeService()
This means employeeService pointcut is applicable to all methods of com.model.service.EmployeeService implementations.
for specific methods we write like
public void doSomeStuff()
This pointcut will pick only this specific method.
And then you define advice where you define actual code about what you want to do,advice is then linked to a pointcut.
public void methodEnter(JoinPoint pjp )
Object obj = pjp.getTarget();
LOG.debug( “[" + obj .toString + "] ” );
So suppose we need to introduce logging into our existing code-base.What are the steps needed?
- Define pointcut’s specifying where these should be invoked. You can use wild-cards and this make pointcut definition very powerful.
- Define an advice as what needs to be done when program flow reaches these pointcuts,i.e our logging code. Now it should be very clear as how we have modularize the cross-cutting concerns.No more we have code like Log.Debug all across out code-base.
- We also has to define as which kind of advice it is.It means when this advice code will get executed. 1) @Before :Runs before the actual method starts running,just after the arguments to the method call are evaluated. 2) After: similarly we have @AfterRetruning,@AfterThrowing and simply @After which is like finally i.e no matter how the method returns ,it will run.
In the example ,the goal is to try and have profiling enabled in the project.Its a very crude profiling which just logs how much time a method takes.
To run this example just create a new AspectJ project in Eclipse,and paste the code in corresponding java files.
TestClass.java is the one which have main method,also if eclipse you can see a nice Cross-Refrence view as which aspect is advising whom or other way round.
Note:Looking out for your comment’s!