Results 1 to 10 of 10

Thread: Spring configured - @Configurable - build time weaving

  1. #1

    Default Spring configured - @Configurable - build time weaving

    Does anyone know of a sample application, or a complete tutorial of how to set this up?
    I have been looking at it now for 3 days without success...

    What Im really trying to do is to inject a service into a jsp tag

    Thanks!

  2. #2
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    You should obtain the knowledge of how to perform build-time aspectj weaving. Good point to start is here - AspectJ documentation. I think that the following chapter covers all your needs - 4. AspectJ Ant Tasks.

    When you're able to perform build-time weaving you can just introduce spring aspect that is responsible for @Configurable processing - AnnotationBeanConfigurerAspect. It's available either at compiled form (as a class org.springframework.beans.factory.aspectj.Annotati onBeanConfigurerAspect) or as an aspect source (AnnotationBeanConfigurerAspect.aj file under spring source distribution).

  3. #3
    Join Date
    Sep 2008
    Location
    London, UK
    Posts
    155

    Default

    You have a number of options to apply aspects at compile time.

    One is the Ant task mentioned.

    If you are using maven you have a maven plugin that works well.

    A plugin for your IDE is also very useful. Eclipse has nice AspectJ integration via AJDT.

    In my current project I am using Eclipse, AJDT and Maven successfully for compile time AspectJ with working debugging etc.

    If you are interested in the maven approach I can provide you with an example. AJDT is really straight forward and documented.

    Cheers

    G

  4. #4
    Join Date
    Sep 2008
    Location
    London, UK
    Posts
    155

    Default

    This is the maven configuration taking care of the @Configurable annotation

    Code:
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.0</version>
      <configuration>
        <source>1.5</source>
        <target>1.5</target>
        <complianceLevel>1.5</complianceLevel>
        <Xlint>ignore</Xlint>
        <aspectLibraries>
          <aspectLibrary>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
          </aspectLibrary>
        </aspectLibraries>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>compile</goal>
            <goal>test-compile</goal>
          </goals>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjrt</artifactId>
          <version>1.6.0</version>
        </dependency>
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjtools</artifactId>
          <version>1.6.0</version>
        </dependency>
      </dependencies>
    </plugin>
    Cheers

    G

  5. #5

    Default

    Ok thank you, I will read up on it. I just wished there would have been a "petclinic" sample application or something ready like that. As Im not really good at either maven or ant it would have helped. Ill have a look at the maven script you sent me Goran, if you have more of a whole application you care to share, you got my mail address on your private messages.

    Thanks!

  6. #6

    Default

    Back again after some more work...

    Still trying to inject a service into a class extending TagSupport, I now get it to compile, without errors, but the injection is not there.
    This is what I think is a working buildtime weaving with iajc

    Code:
    	<taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties">
        	<classpath>
          		<pathelement location="/home/johan/utveckling/lib/aspectjtools-1.6.0.jar"/>
        	</classpath>
      	</taskdef>
    	
    	<target name="compaspect">
    		<echo message=" **** Compiling aspect "/>
    		<mkdir dir="${build}/WEB-INF/classes"/>
    		<iajc 
    			argfiles="ajc.args.lst"
    			srcdir="src"
    			destdir="${build}/WEB-INF/classes"
    			classpathref="compile.class.path" 
    			debug="true" 
    			deprecation="false" 
    			failonerror="true"/>
        </target>
    the spring settingfile

    Code:
    <context:spring-configured/>
    <context:annotation-config/>
    
    <bean class="com.work.web.tags.ContentTag" scope="prototype" >
        	<property name="contentService" ref="contentService"/>
    </bean>
    
    <bean id="contentService" class="com.work.services.impl.ContentServiceImpl"/>
    and the Jsp tag


    Code:
    
    @Configurable(dependencyCheck=true)
    public class ContentTag extends TagSupport {
    
    	private ContentService contentService;
    
    	@Override
    	public int doEndTag() throws JspException {
    		
    		HttpServletRequest request =
    			(HttpServletRequest)pageContext.getRequest();
    
    
    		try {
    			pageContext.getOut().println("Service: " + contentService);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		return super.doStartTag();
    	}
    
    	public void setContentService(ContentService contentService) {
    		this.contentService = contentService;
    	}
    }
    Printout is just "Service: null "

    Please help me here! What have I done wrong?

    Thanks!

  7. #7
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Quote Originally Posted by sore View Post
    ...
    Printout is just "Service: null "

    Please help me here! What have I done wrong?

    Thanks!
    1. Check that aspect logic is really woven into your tag class. You may have that done via disassembling and checking byte code, e.g. with javap utility tool. You should expect to see something like
      com.work.web.tags.ContentTag();
      Code:
      0: aload_0
      1: invokespecial #10; //Method javax/servlet/jsp/tagext/TagSupport."<init>")V
      4: getstatic #146; //Field ajc$tjp_1:Lorg/aspectj/lang/JoinPoint$StaticPart;
      7: aload_0
      8: aload_0
      9: invokestatic #102; //Method org/aspectj/runtime/reflect/Factory.makeJPLorg/aspectj/lang/JoinPoint$StaticPart;Ljava/lang/Object;Ljava/lang/Obje
      ctLorg/aspectj/lang/JoinPoint;

      ...
      many, many more
    2. Make sure spring context is initialized when your tag object is being instantiated. You can check that by performing logging from the tag constructor and checking the order of spring context initialization and tag object creation. If your object is instantiated before context initialization nothing is injected;

  8. #8
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    I notice that you are not providing the aspectpath attribute to iajc. This attribute should point to spring-aspects.jar. See http://www.eclipse.org/aspectj/doc/r...asks-iajc.html for more details.

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  9. #9

    Default

    Thanks again for the help!

    So I tried javap, and it doesnt look good, no aspectj here:

    Code:
    public class com.work.web.tags.ContentTag extends javax.servlet.jsp.tagext.SimpleTagSupport{
    public com.work.web.tags.ContentTag();
      Code:
       0:   aload_0
       1:   invokespecial   #13; //Method javax/servlet/jsp/tagext/SimpleTagSupport."<init>":()V
       4:   return
    
    public com.work.web.tags.ContentTag(com.work.services.ContentService);
      Code:
       0:   aload_0
       1:   invokespecial   #13; //Method javax/servlet/jsp/tagext/SimpleTagSupport."<init>":()V
       4:   aload_0
       5:   aload_1
       6:   putfield        #20; //Field contentService:Lcom/work/services/ContentService;
       9:   return
    
    public void doTag()   throws java.io.IOException;
      Code:
       0:   aload_0
       1:   invokevirtual   #26; //Method getJspContext:()Ljavax/servlet/jsp/JspContext;
       4:   invokevirtual   #30; //Method javax/servlet/jsp/JspContext.getOut:()Ljavax/servlet/jsp/JspWriter;
       7:   new     #36; //class java/lang/StringBuilder
       10:  dup
       11:  ldc     #38; //String Service injected:
       13:  invokespecial   #40; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
       16:  aload_0
       17:  getfield        #20; //Field contentService:Lcom/work/services/ContentService;
       20:  invokevirtual   #43; //Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
       23:  ldc     #47; //String
       25:  invokevirtual   #49; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
       28:  aload_0
       29:  getfield        #52; //Field name:Ljava/lang/String;
       32:  invokevirtual   #49; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
       35:  ldc     #54; //String <br />
       37:  invokevirtual   #49; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
       40:  invokevirtual   #56; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
       43:  invokevirtual   #60; //Method javax/servlet/jsp/JspWriter.write:(Ljava/lang/String;)V
       46:  return
    
    public void setName(java.lang.String);
      Code:
       0:   aload_0
       1:   aload_1
       2:   putfield        #52; //Field name:Ljava/lang/String;
       5:   return
    
    public void setContentService(com.work.services.ContentService);
      Code:
       0:   aload_0
       1:   aload_1
       2:   putfield        #20; //Field contentService:Lcom/work/services/ContentService;
       5:   return
    
    }

    Here is the iajc part of the ant script

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <project name="minimal" default="all"  >
    
    		<import file="properties.xml"/>
    
    		<path id="compile.class.path">
    			<fileset dir="${tomcat.home}/common/lib" includes="*-api.jar"/>
    			<fileset dir="war/WEB-INF/lib" includes="**/*.jar"/>
    			<pathelement path="${build}/WEB-INF/classes" />
    			<pathelement path="${classpath}" />
    		</path>
    
    		<taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties">
    	    	<classpath>
    	      		<pathelement location="${aspect.home}aspectjtools-1.6.0.jar"/>
    	    	</classpath>
    	  	</taskdef>
    		
    		<target name="compaspect">
    			<echo message=" **** Compiling aspect "/>
    			<mkdir dir="${build}/WEB-INF/classes"/>
    			<iajc 
    				argfiles="ajc.args.lst"
    				srcdir="src"
    				destdir="${build}/WEB-INF/classes"
    				classpathref="compile.class.path" 
    				debug="true" 
    				deprecation="false" 
    				failonerror="true"/>
    	    </target>
    It doesnt complain about anything when compiling, ant it works, it just doesnt inject the service...
    Any suggestions?

    EDIT: just seen your answer ramnivas. Ill check it out and return

  10. #10

    Default

    Thanks very much, it works!!
    The last thing to fix was what ramnivas wrote.

    here is the antscript if anyone needs it (I dont know if its the 'right' way to do it, but it works for me)

    Code:
    <project name="minimal" default="all"  >
    
    		<import file="properties.xml"/>
    
    		<path id="aspect.path">
    	        <pathelement path="${aspect.home}spring-aspects.jar"/>
    	    </path>
    
    	
    		<path id="compile.class.path">
    			<fileset dir="${tomcat.home}/common/lib" includes="*-api.jar"/>
    			<fileset dir="war/WEB-INF/lib" includes="**/*.jar"/>
    			<pathelement path="${build}/WEB-INF/classes" />
    			<pathelement path="${classpath}" />
    		</path>
    
    		<taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties">
    	    	<classpath>
    	      		<pathelement location="${aspect.home}aspectjtools-1.6.0.jar"/>
    	    	</classpath>
    	  	</taskdef>
    		
    		<target name="compaspect">
    			<echo message=" **** Compiling aspect "/>
    			<mkdir dir="${build}/WEB-INF/classes"/>
    			<iajc 
    				 compliance="-1.5" 
    				srcdir="src"
    				destdir="${build}/WEB-INF/classes"
    				classpathref="compile.class.path" 
    				debug="true" 
    				deprecation="false" 
    				failonerror="true">
    				<aspectpath refid="aspect.path"/>    
    
    			</iajc>
    	    </target>
    aspectjtools and aspcetjrt had to be the same version.
    Ok, exiting, I have a few ideas to test now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •