how to setup load-time weaving with Spring and Tomcat WITHOUT using the -javaagent on the command line
<p>I am using Spring 3.2.9, Tomcat 6.0.44</p>

<p>I am trying to configure my application's Spring instrumentation provider (e.g. spring-instrumentation.jar) for load-time weaving, when it is deployed on Tomcat.</p>

<p>I have a requirement to NOT use:
"-javaagent:/path/path/spring-instrument.jar" on the command line to do the configuration.</p>

<p>I've read that I can configure the Spring instrumentation by modifying the &lt;Context&gt; element of my application's Tomcat configuration (in either Tomcat's server.xml or my web app's context.xml). Adding the appropriate &lt;Context&gt; element to the server.xml results in my application being correctly configured to run with Spring's instrumentation provider. Adding it to the context.xml (see below) does NOT result in a working setup.</p>

<p>I have a META-INF/aop.xml file, looks like this:</p>

<pre><code>    &lt;aspectj&gt;
        &lt;weaver options="-verbose -showWeaveInfo -debug"&gt;
            &lt;include within="com.mv.xx.services..*"/&gt;
            &lt;exclude within="com.mv.xx.aop..*"/&gt;
        &lt;/weaver&gt;
        &lt;aspects&gt;
            &lt;aspect name="com.mv.xx.aop.MyAspect"/&gt;
        &lt;/aspects&gt;
    &lt;/aspectj&gt;
</code></pre>

<p>I also specify that I want to use load-time weaving by adding this to my Spring context config:</p>

<pre><code> &lt;context:load-time-weaver /&gt;
</code></pre>

<p>And I add this jar to my application's classpath:
spring-instrument-tomcat.jar</p>

<p>WHAT I HAVE TRIED:</p>

<p>When starting Tomcat, If I identify the location of the spring-instrument.jar on the command line using the -javaagent parameter like this:</p>

<pre><code>-javaagent:/path/path2/spring-instrument-3.2.9.RELEASE.jar
</code></pre>

<p>Everything works fine.</p>

<hr>

<p>Next I removed "-javaagent:/path/path2/spring-instrument-3.2.9.RELEASE.jar" from the command line.
In Tomcat's server.xml file (located in $CATALINE_HOME/conf), I added a a &lt;Context&gt; element to the &lt;Host&gt; element, like this:</p>

<pre><code>&lt;Context path="/myApp" docBase="myApp"&gt;
    &lt;Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/&gt;
&lt;/Context&gt;
</code></pre>

<p>With this configuration everything behaves properly. However I have a requirement to not modify Tomcat's server.xml, since I don't have control over the server.xml (DevOps does, and is reluctant to modify it).</p>

<hr>

<p>Next I removed the &lt;Context&gt; element from Tomcat's server.xml.
According to the <a href="http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-aj-ltw-environments" rel="noreferrer">Spring docs</a>, I can add a /META-INF/context.xml to my webapp, and put the &lt;Context&gt; element that used to be in Tomcat's server.xml into the context.xml, like so:</p>

<pre><code>        &lt;Context&gt;
            &lt;Context path="/myApp" docBase="myApp"&gt;
                &lt;Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/&gt;
            &lt;/Context&gt;
        &lt;/Context&gt;
</code></pre>

<p>However when I restart Tomcat, I get an error message in the logs saying:</p>

<pre><code>    SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.weaving.AspectJWeavingEnabler#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver': Initialization of bean failed; nested exception is java.lang.IllegalStateException: ClassLoader [org.apache.catalina.loader.WebappClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar
</code></pre>

<p>After digging around, I read something that suggested that I modify the <code>&lt;context:load-time-weaver/&gt;</code> element in my Spring config, like this:</p>

<pre><code>        &lt;context:load-time-weaver weaver-class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /&gt;
</code></pre>

<p>And add the jar containing InstrumentationLoadTimeWeaver.class to my classpath.</p>

<p>However when I do that, I get this error message in the logs:</p>

<pre><code>        SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
        java.lang.IllegalStateException: Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation.
    etc....
</code></pre>

<p>Can anyone explain how to setup load-time weaving with Spring and Tomcat WITHOUT using the -javaagent on the command line, and WITHOUT modifying the server.xml?</p>