Announcement Announcement Module
Collapse
No announcement yet.
Problems with aspectj LTW Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

    Code:
    <beans
    	xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans
    		http://www.springframework.org/schem...ring-beans.xsd
    		http://www.springframework.org/schema/aop
    		http://www.springframework.org/schem...ng-aop-2.5.xsd
    		http://www.springframework.org/schema/context
    		http://www.springframework.org/schema/context/spring-context.xsd">	
    
    	<context:spring-configured />
    	<aop:aspectj-autoproxy />
    	<context:load-time-weaver />
    
    	<bean class="com.icelink.core.Person" scope="prototype">
    		<property name="name" value="Goutham"/>
    	</bean>
    
    </beans>
    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!
    Last edited by gpulluri; Feb 29th, 2012, 08:57 AM. Reason: adding version info

  • #2
    For starters use [ code][/code ] tags when posting code, that way it remains readable.

    Next your around advice destroys your call flow you should ALWAYS reteurn the result of the proceed method and not a void method. Next you have aspectj-autoproxy but haven't defined the aspect, so basically you don't have aspects. And finally make sure you use the person from the context and not a new instance.

    Comment


    • #3
      I think its not picking up the aop.xml

      Originally posted by Marten Deinum View Post
      For starters use [ code][/code ] tags when posting code, that way it remains readable.
      Done

      Originally posted by Marten Deinum View Post
      Next your around advice destroys your call flow you should ALWAYS reteurn the result of the proceed method and not a void method.
      Done

      Originally posted by Marten Deinum View Post
      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.

      Originally posted by Marten Deinum View Post
      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 ?

      Comment


      • #4

        Can you please elaborate on that? I defined a class and annotated it as an aspect. Does that not qualify as an aspect ?
        It qualifies it as an aspect but not as an aspect that is used. Spring will only apply aspects it knows about your aspect just sits on the class path doing nothing.

        Also you defined the pointcut around an annotation however that annotation is nowhere used in your code which makes it pretty much a useless pointcut and thus aspect.

        Comment


        • #5
          Originally posted by Marten Deinum View Post
          It qualifies it as an aspect but not as an aspect that is used. Spring will only apply aspects it knows about your aspect just sits on the class path doing nothing.

          Also you defined the pointcut around an annotation however that annotation is nowhere used in your code which makes it pretty much a useless pointcut and thus aspect.
          I am declaring the aspect in the 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>
          I noticed that I wasn't using the annotation anywhere so I changed that. The second attachment of the code has the modified version.

          Code:
          
          package com.icelink.core;
          
          import org.springframework.beans.factory.annotation.Configurable;
          
          import com.icelink.annotation.Name;
          
          @Configurable
          public class Person {
          	
          	private int id;
          	private String name;
          	
          	
          	public int getId() {
          		return id;
          	}
          	public void setId(int id) {
          		this.id = id;
          	}
          	
          	@Name
          	public String getName() {
          		return name;
          	}
          	public void setName(String name) {
          		this.name = name;
          	}
          	
          }
          am I still missing something?

          Comment


          • #6
            Can someone please give me a Helloworld kind of program with aspectj and tomcat ?

            Comment

            Working...
            X