AspectJ Load-time Weaving in Spring

David Winterfeldt

2008


This example shows using Spring with AspectJ's Load-time Weaving (LTW) to gather statistics. Spring 2.5 added support to make it easier to use AspectJ's LTW. It's also finer grained because you can activate it in specific classloaders using the new context namespace by adding this, <context:load-time-weaver/>, to your XML configuration. Besides adding a JVM argument and the custom namespace, the rest is standard AspectJ LTW and how to configure the AspectJ configuration file can be found on AspectJ's site.

[Note]Note

This example must be run with Java 5 or higher since support for the -javaagent JVM argument was added in Java 5. Also, Maven and Eclipse are currently configured to look for the spring-agent.jar at c:/spring-framework-2.5/dist/weaving/spring-agent.jar so you will need to edit this to match where it is actually located. This can be changed at the bottom of the Maven pom.xml or in the Eclipse Run Dialog's argument tab.

1. JVM Argument

This JVM argument needs to be set in order for Spring's context:load-time-weaver to work, although with certain classloaders this isn't necessary.

                
        	       -javaagent:/path/to/jar/spring-agent.jar