Friday, October 12, 2012

Demystifying AOP, Getting started with LTW (load time weaving)

Often, using aspectj AOP, especially with LTW (load time weaving) is shrouded in mystery.
Thought I would write up a little note about getting started with Aspectj AOP LTW.

here goes...all you would need is a simple eclipse java project looking like below.


We would need 
  1. an aspect source file - MySimpleLoggerAspect.java
  2. a sample service which will get AOP-ed - SampleService.java
  3. a test class with main method - Tester.java
  4. an Aspectj, LTW related config file - META-INF\aop.xml
  5. aspectjrt-1.7.0.jar and aspectjweaver-1.7.0.jar in your project classpath


MySimpleLoggerAspect.java is the logging aspect. For further details about writing aspectj aspects please refer www.eclipse.org/aspectj/docs.php. Listing of the simple aspect in java is below.

----------------------------------------------------------------
package com.ghag.rnd.aspects.ltw;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class MySimpleLoggerAspect {
@Pointcut("execution(* *(..))")
public void myTraceCall() {
}
@Around("com.ghag.rnd.aspects.ltw.MySimpleLoggerAspect.myTraceCall()")
public Object myTrace(ProceedingJoinPoint joinPoint) throws Throwable
{
System.out.println("myTrace:before call "
+joinPoint.getTarget().getClass().getName()
+"."+joinPoint.getSignature().getName());
Object retVal = null;
try
{
retVal = joinPoint.proceed();
}
finally
{
System.out.println("myTrace:after call "+
joinPoint.getTarget().getClass().getName()
+"."+joinPoint.getSignature().getName() + " retval=" +retVal);
}
return retVal;
}


}




SampleService.java is as easy as given below:
----------------------------------------------------------------
package com.ghag.rnd.aspects.sample;

public class SampleService {
public String doService(String in){
System.out.println("inside doService");
return in;
}
}




Tester.java listing is a few more lines of code:
----------------------------------------------------------------
package com.ghag.test;

import com.ghag.rnd.aspects.sample.SampleService;

public class Tester {
public static void main(String[] args) {
new SampleService().doService("Ganesh Ghag");
}
}




And finally the META-INF\aop.xml listing is simple a self explanatory, especially the package names ;-)
----------------------------------------------------------------
<aspectj>
<aspects>
<aspect name="com.ghag.rnd.aspects.ltw.MySimpleLoggerAspect" />
</aspects>
 
<!--  <weaver options="-verbose -debug -showWeaveInfo"> -->
<weaver>
<include within="com.ghag.rnd.aspects.sample.*" />
<include within="com.ghag.rnd.aspects.ltw.*" />
</weaver>
</aspectj>





Now when you run Tester.java, just ensure you have the following paramater supplied as JVM argument:
-javaagent:/your dev env local /path/to/aspectj\aspectjweaver-1.7.0.jar

Thats it folks, when you run, Tester.java, SampleService call will get AOP-ed and give following output:
----------------------------------------------------------------
myTrace:before call com.ghag.rnd.aspects.sample.SampleService.doService
inside doService
myTrace:after call com.ghag.rnd.aspects.sample.SampleService.doService retval=Ganesh Ghag


Getting started with AspectJ AOP with LTW is that easy, folks!





28 comments:

Anonymous said...

not working at all

Anonymous said...

Not Working .

Anonymous said...

Not working...

ragul ragul said...

Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
Best Devops Training in pune
Microsoft azure training in Bangalore
Power bi training in Chennai

tamizh said...

I really like the dear information you offer in your articles. I’m able to bookmark your site and show the kids check out up here generally. Im fairly positive theyre likely to be informed a great deal of new stuff here than anyone
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training

Aliya Manasa said...

I am definitely enjoying your website. You definitely have some great insight and great stories. 
python Course in Pune
python Course institute in Chennai
python Training institute in Bangalore

sasitamil said...

Hello I am so delighted I found your blog, I really found you by mistake, while I was looking on Yahoo for something else, anyways I am here now and would just like to say thanks for a tremendous post. Please do keep up the great work.
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training

Vignesh said...
This comment has been removed by the author.
Priyanka said...

Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
python training in bangalore

Anonymous said...

Visit for AI training in Bangalore:- Artificial Intelligence training in Bangalore

Realtime Experts said...

Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts.Informatica Training in Bangalore

Realtime Experts said...

Awesome,Thank you so much for sharing such an awesome blog.Big Data Training in Marathahalli

Anonymous said...

Oh man...you are such a fake guy.You even have your minions and fake accounts to comment for you with your links.At least have the decency to post genuine stuff.You may remove my comment man but I just wanted you to see this.

Anonymous said...

Thank you for the example. For those its not working please add jar -javaagent:YOURJARPATH\aspectjweaver-1.9.5.jar

Keerthana said...

Great Jobs are Done Through these Blogs, very Interesting to convey the specific contents via These Articles.Thanks for sharing
python training in chennai | python training in annanagar | python training in omr | python training in porur | python training in tambaram | python training in velachery

Anu said...

Your posts is really helpful for me.Thanks for your wonderful post. I am very happy to read your post.
devops training in chennai | devops training in anna nagar | devops training in omr | devops training in porur | devops training in tambaram | devops training in velachery

harshni said...

This is the exact information I am been searching for, Thanks for sharing the required infos with the clear update and required points. To appreciate this I like to share some useful information.
Artificial Intelligence Training in Chennai

Ai Training in Chennai

Artificial Intelligence training in Bangalore

Ai Training in Bangalore

Artificial Intelligence Training in Hyderabad | Certification | ai training in hyderabad

Artificial Intelligence Online Training

Ai Online Training

Blue Prism Training in Chennai

lavanya said...

Wow it is really wonderful and awesome thus it is veWow, it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot.
Java training in Chennai

Java Online training in Chennai

Java Course in Chennai

Best JAVA Training Institutes in Chennai

Java training in Bangalore

Java training in Hyderabad

Java Training in Coimbatore

Java Training

Java Online Training

anand said...

nice post
Software Testing Training in Chennai | Certification | Online
Courses



Software Testing Training in Chennai

Software Testing Online Training in Chennai

Software Testing Courses in Chennai

Software Testing Training in Bangalore

Software Testing Training in Hyderabad

Software Testing Training in Coimbatore

Software Testing Training

Software Testing Online Training

Ramya said...

It’s hard to come by experienced people about this subject, but you seem like you know what you’re talking about! This concept is a good way to enhance the knowledge.thanks for sharing.
DevOps Training in Chennai

DevOps Online Training in Chennai

DevOps Training in Bangalore

DevOps Training in Hyderabad

DevOps Training in Coimbatore

DevOps Training

DevOps Online Training

prabhu said...

A huge thanks to you for sharing this Blog your blog content is very nice ,I have read your blog your blog information is very useful.
IELTS Coaching in chennai

German Classes in Chennai

GRE Coaching Classes in Chennai

TOEFL Coaching in Chennai

spoken english classes in chennai | Communication training


vivekvedha said...

Wow it is really wonderful and awesome thus it is veWow, it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot.
acte chennai

acte complaints

acte reviews

acte trainer complaints

acte trainer reviews

acte velachery reviews complaints

acte tambaram reviews complaints

acte anna nagar reviews complaints

acte porur reviews complaints

acte omr reviews complaints

radhika said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.


AWS Course in Bangalore

AWS Course in Hyderabad

AWS Course in Coimbatore

AWS Course

AWS Certification Course

AWS Certification Training

AWS Online Training

AWS Training

Lavanya said...

Good Post! , it was so good to read and useful to improve my knowledge as an updated one, keep blogging.After seeing your article I want to say that also a well-written article with some very good information which is very useful for the readers.....
salesforce training in chennai

software testing training in chennai

robotic process automation rpa training in chennai

blockchain training in chennai

devops training in chennai

Himachali Khabarnama said...

Annabelle loves to write and has been doing so for many years.Backlink Indexer My GPL Store Teckum-All about Knowledge

Deekshitha said...

Informative blog
machine learning training in kolkata

Unknown said...

Good work.
It took me ca. 15 Minutes just to recreate the project, download the libs and run it.
Works like a charm.
To all the guys that claim that I#s not working: C'mon guys, use you heads, maybe you have to adjust a little bit the path to your jar after "javaagent:"

data science course in patna said...

It has increases the importance of the field. If you have also been attracted by the value of data science and want to learn it, you have to understand the basic courses of data science you need to learn to move successfully in the industry.
data science course in patna