Basically, to get AspectJ Load Time Weaving to work, you first need to think about the environment in which your application will run. As the name says, βload timeβ means that your classes will be modified by the class loader. Therefore, from your question, I understand that you need your LTW in two environments. Firstly, this is a "test" environment, which is a command line, and the second is a servlet container (Jetty / Tomcat).
So, let's start with the test environment: your tests fail because the classloader does not have the ability to edit / write / drag classes, Spring cannot help you because it is not already running, and you chose Load Time over the weaving time. Therefore, we need to somehow inform the default class loader that we will crack classes. This is documented in Spring Documentation. For this, we need to add -javaagent , which in our case, instead of tools / profiling, the classes will simply change at boot time. To do this, just tell maven to add 2 javaaganets : aspectj-weaver-x.jar and special spring-instrument-x.jar , so just add plugins maven section.
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> <forkMode>once</forkMode> <argLine> -javaagent:${settings.localRepository}/org/springframework/spring-instrument/4.2.2.RELEASE/spring-instrument-4.1.6.RELEASE.jar -javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/1.8.7/aspectjweaver-1.8.7.jar </argLine> <useSystemClassloader>true</useSystemClassloader> </configuration> </plugin>
Remember to add spring-instrument-4.1.6.RELEASE.jar as a dependency, at least in the provided , to make sure it is in the repo;) After adding these configuration parameters that you execute, execution will work fine . Just try:
mvn clean package
It starts from the second environment in the servlet container. If you use Tomcat, then you are happy, as you can override the default bootloader with the one that comes with Spring by adding the following META-INF/context.xml to the project.
<Context path="/myWebApp" docBase="/my/webApp/location"> <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/> </Context>
More details here . For Jetty, you must manually edit the run script and add two -javaagent , as was done for the maven test environment.