Load-time weaving, Spring and Maven.

As some of you might have read in my earlier post, I’m using load-time weaving in the project that I’m working on. Lately I’ve run in to some problems with getting the tests to play nice with Maven.

So what was the problem? Well, I’ve been using @Configurable and @Autowired to inject stuff in my domain object which I create in code. To get this working when running mvn test you have to pass in spring-agent.jar as javaagent. When you do this, everything worked great! To to this with Maven you have to do the following:


  org.apache.maven.plugins
    maven-surefire-plugin
    2.4
    
      once
      
        -javaagent:${settings.localRepository}/org/springframework/spring-agent/${spring.version}/spring-agent-${spring.version}.jar
      
      true
    

Now, that’s all cool, and it worked like a charm… for a while. When I added @Configurable to another class to autowire in a new dependency, I could not get that specific class to get weaved properly. Believe me, I tried everything I could think of, but nothing worked… until I found a hint on a thread with a related problem. By passing in a second javaagent, aspectjweaver.jar, the problem was solved! To do this in Maven you just add the second javaagent after the first, like this:


  org.apache.maven.plugins
    maven-surefire-plugin
    2.4
    
      once
      
        -javaagent:${settings.localRepository}/org/springframework/spring-agent/${spring.version}/spring-agent-${spring.version}.jar -javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/1.6.1/aspectjweaver-1.6.1.jar
      
      true
    

Note: If you add a line-break between the two javaagents, it doesn’t work.

What was the problem I had? I have no idea! Why does this solution work? I have no idea! All I know is that I get a green light from my tests, and that I don’t need this solution when running live in Tomcat or whatever, since the classloader there has addTransformer(ClassFileTransformer).

Still, if anyone has any idea of why I, after everything worked with only spring-agent.jar, needed to add aspectjweaver I am all ears! Don’t get me wrong! I can live with not knowing, but I would get peace of mind from knowing…

This Post Has 5 Comments

  1. The same procedure is required with Eclipse Helios.
    Thx for sharing this information!

  2. Thank you for this post. It solved my problem with aspectjweaver.jar. I used the weaving to inject properties from template bean to my bean created in local code. For this I needed aspectjweaver.jar and spring 2.0 config . I see now it is not any more provided in spring 3.1.

  3. Wow man, that tip just saved my week!
    Thanks a lot!

  4. We have this problem as well, but only with TestNG. We solved it with the help of this post. To us, it’s a problem with TestNG + Spring LTW, not Maven, as it occurs whenever we try to execute a test from within the IDE. This solved the problem with Eclipse, but I am struggling to get it to work in IntelliJ IDEA (which I’ve just started using). It sort of works in that a lot more of the tests work now, but some are still failing with missing injected properties.

  5. Great work mate, Thanks for sharing these info. It saved my much of work.

Leave a Reply

Close Menu