Aspect Oriented Programing With Spring

Yesterday I got a requirement, where I need to add logging in more then 500 existing classes :-(

How could I achieve this ??

of course...

I need to open every file and add some code like


log.info("Beware : ");

a bit labours work isn't it ? and you know what ? my boss wants me to complete these writing 2-3 days :-(

He provided me 500 .java files and ask me to write code so when ever an exception occurred that can be logged in log file.


I am very nervous, sad, irritate.. because of these tedious task and seat in to my cubical with unhappy face.


and then a magic happened.


an angle appears in my computer screen wearing green dress and having a feather in her hand, she said,

"Don't worry Ishan ! I am here to rescue you from your Boss !!!"
"You don't need even a day to complete your task, I'll help you with
my magical configuration & reflaction."

"May I know your name please ?"

"Spring !"
"And these is my AOP feather !"

"But you know, I hate to learn so much of theories. :-("
"Hey I am not here to teach you anything okey... I am here to help you, guide you in very simple way my child !"
"Oooo really ??"

"Yes, Baby. So shall we start working now ?"
"Why not my pleasure :-)"

And finnally we come to one class which intecepts all my 500 classes and put the logging stuff dynamically. So what I need is just
one class with few lines of code and few lines of configuration tht's it.

The logging mechanisam for 500 classes is ready.

I note down my experience of AOP with Spring in following lines.


First of all we need a class where we write some logging stuff. let's say
LoggingInterceptor.

LoggingInterceptor.java

"hey, I don't understand anything other then the getStackTrace(Exception ex) method."
"Don't worry ! let me clear everything is this code."

Let's assume that we are using apache's log4j mechanism for logging.
line no. 7 : imported the Logger of apache.
line no. 8 : imported the ThrowsAdvice of Spring's AOP framework.

Spring is having different types of advices but presently we look for ThrowsAdvice only and later work around the other advices like BeforAdvice, AfterReturnAdvice & AroundAdvice.

ThrowsAdvice :

It is a marker interface which says execute this chunk of code if any exceptions occured.

So when an exception is being thrown
afterThrowing(Method method, Object[] args, Object target, Throwable throwable) method is being called. See the segnature of this method.

Here using reflaction your desired chunk of code is being added and ofcourse executed.

"Okey ! but not fully cleared yet. What I understand is that we write some code in a class which we want to implement in all 500 classes. But I am not cleared that how this will executed at runtime ? How the spring will come to know that where to add this
code and where to not ?"

"Just Relax ! We discuss everything..."

line no. 10 : Our LoggingInterceptor implements the ThrowsAdvice and get the power of Spring AOP.
line no. 13 : Provide the implementation of
afterThrowing(Method method, Object[] args, Object target, Throwable throwable) method.

As our problem is to implement the logging stuff in all the service layered implementations (500 classes).
and my packages are like...


com.springtutorial.aop.service.impl
and in side this package all my 500 classes reside.

As we know in spring we need to create the beans. So let we create a bean of our LoggingInterceptor.

application-context.xml

Now the question come that how spring come to know that what and where to do ?

So what we want to spring to do ?
1. find all the classes available under
com.springtutorial.aop.service.impl package only.
2. find the class where we write the Logging stuff.
3. combine both the things at runtime if any exception thrown.

Let's modify our configuration file...
application-context.xml

Please note that
line no
. 4 : we added name space for aop. line no. 7 : added schema location for aop. line no. 11 : Special used a feature provided in 2.x version for aop configuration. line no. 12 : Define a pointcut What is a point cut ? A point cut defines a specific place where you want to add the cross cutting functionalities in our case that is writing logs. the execution attribute of pointcut tag accepts the regex as the arguments. Here we can go upto method level as well but presently we only go up to a package level. line no. 13 : Define a advisor What is an advisor ? An advisor is what brings the advise(LoggingInterceptor) and the pointcut(500 classes) brings together. The spring will create a proxy having functionalities of both the classes at run time.

And nowwwwwwwwww

hey your AOP stuffs has been completed..
let's test it.

We are having one ProblematicServiceImpl which throws a RunTimeException.

ProblematicServiceImpl.java

and we have a main class

TestMain.java

So when we run our TestMain, an ArithmeticException is being thrown by the code and the details of this exception is eing written automatically in our log file using Spring AOP.

Awesome Framework !!!
isn't it ?

It reduces our work a lot, and provide the space to more concentrate on business needs of the applications.

No comments:

Post a Comment