Enabling load-time weaving with Tomcat

I gathered the following information from Spring’s documentation (http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-aj-ltw), the Maven-aspectj plugins’s site (http://mojo.codehaus.org/aspectj-maven-plugin/) and the ant task’s reference (http://www.eclipse.org/aspectj/doc/next/devguide/antTasks-iajc.html#antTasks-iajc-options). Hope you find it useful to have it all in one place.

  • Disable compile-time weaving in build environment (flag: XterminateAfterCompilation), and have it generate aop-ajc.xml (flag: outxml) to list your own aspects that are compiled:
    • maven
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
  [...]
  <executions>
    <execution>
        <goals>
            <goal>compile</goal>
            <goal>test-compile</goal>
        </goals>
    </execution>
  </executions>
  <configuration>
    <!-- the important part -->
    <outxml>true</outxml>**
    <XterminateAfterCompilation>true</XterminateAfterCompilation>
    [...]
  </configuration>
</plugin>
  • ant
<target name="-java-compile" depends="-java-init, -aspectj-init" extensionOf="-do-compile">
  [...]
  <iajc classpathref="compile.class.path"
        aspectpathref="compile.class.path" destdir="${build.dir}/classes"
        source="${javac.source}" target="${javac.target}"
        outxml="true" XterminateAfterCompilation="true">
    <src refid="source.path"/>
  </iajc>
  [...]
</target>
  • Enable load-time weaving in applicationContext.xml:
<context:load-time-weaver/>
  • In context.xml of your webapp, specify the weaving-enabled classloader:
<Context path="/rm" docBase="rm" reloadable="true" crossContext="false">
<!-- the important part -->
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"
    useSystemClassLoaderAsParent="false"/>
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
  <weaver options="**-verbose -showWeaveInfo">
    <include within="com.yourcompany..*"/>
  </weaver>
</aspectj>

As far as I know, you cannot disable compile-time weaving in Eclipse. Maybe this is the solution, I’ll have to check: http://wiki.eclipse.org/JDT_weaving_features