1 Attachment(s)
Problems with aspectj LTW
Hi All,
I am trying to build an application using aspectj LTW to profile a tomcat web project. However, I am new to web development and finding it difficult to even get a basic Helloworld kind of application running. Inspite of going through numerous posts on the subject I still have no success. I am hoping someone here might be able to help me out.
I am using tomcat 6.0.35, spring 2.5.6 and aspectj 1.6.12
I am using the below jars which I am not including in the attachment.
aopalliance-1.0.jar
aopalliance-alpha1.jar
aspectjrt.jar
aspectjtools.jar
aspectjweaver.jar
org.aspectj.matcher.jar
spring-aop.jar
spring-aspects.jar
spring-beans.jar
spring-context-support.jar
spring-context.jar
spring-core.jar
spring-web.jar
Here's what I am doing (the entire source code is attached excl the jars):
1) created an annotation called Name
Code:
package com.icelink.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Name {
}
2) created an aspect. I am trying to add a "Mr." to the name using LTW
Code:
package com.icelink.aspects;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class MyAspect {
@Around(value="@annotation(com.icelink.annotation.Name) && args(name)")
public void printInfo(ProceedingJoinPoint pjp, String name) throws Throwable{
name = "Mr."+name;
pjp.proceed(new Object[]{name});
}
}
3) created a simple class
Code:
package com.icelink.core;
import org.springframework.beans.factory.annotation.Configurable;
@Configurable
public class Person {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4) created applicationContext.xml
5) Created the rest of the folders like WEB-INF(web.xml) META-INF(aop.xml etc)
Note: Even though there is a context.xml under META-INF I have the context in server.xml(also incl in the source code) so it the one under META-INF won't be picked.
aop.xml
Code:
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<weaver>
<!-- only weave classes in our application-specific packages -->
<include within="com.icelink.core.*" />
</weaver>
<aspects>
<!-- weave in just this aspect -->
<aspect name="com.icelink.aspects.MyAspect" />
</aspects>
</aspectj>
6) Created a simple index.jsp which calls the getName on the person object. Here I am expecting the LTW to add a Mr. to the name. But that doesn't happen. I just see a Hello null!
1 Attachment(s)
I think its not picking up the aop.xml
Quote:
Originally Posted by
Marten Deinum
For starters use [ code][/code ] tags when posting code, that way it remains readable.
Done
Quote:
Originally Posted by
Marten Deinum
Next your around advice destroys your call flow you should ALWAYS reteurn the result of the proceed method and not a void method.
Done
Quote:
Originally Posted by
Marten Deinum
And finally make sure you use the person from the context and not a new instance.
Done. Created a servlet which gets the person from the context.
Quote:
Originally Posted by
Marten Deinum
Next you have aspectj-autoproxy but haven't defined the aspect, so basically you don't have aspects.
Can you please elaborate on that? I defined a class and annotated it as an aspect. Does that not qualify as an aspect ?
I am attaching the latest code with the modification as you suggested. Its still not working.
I am pretty positive the aop.xml is not getting picked up. Can someone please take a look at the code (attached) and tell me what I am doing wrong ?