How to configure load-time weaving with AspectJ and Tomcat?

Go To StackoverFlow.com

8

I tried to configure load-time weaving (for doing profiling with Perf4J) in the next way:

1) I added aop.xml to META-INF folder. When deployed, META-INF is placed in the artifact root directory (i.e. MyAppDeployed/META-INF).

2) I put aspectjrt-1.6.1.jar, aspectjweaver-1.6.1.jar, commons-jexl-1.1.jar, commons-logging.jar to the Tomcat/lib folder (at first I tried MyAppDeployed/WEB-INF/libs but it also didn't work).

3) I added -javaagent:C:\apache-tomcat-6.0.33\lib\aspectjweaver-1.6.1.jar to VM options when starting Tomcat.

4) My aop.xml:

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">

<aspectj>

    <aspects>
        <aspect name="org.perf4j.log4j.aop.TimingAspect"/>
    </aspects>

    <weaver options="-verbose -showWeaveInfo">           
        <include within="com.mypackages.MyClass"/>
    </weaver>
</aspectj>

I don't see any signs that load-time weaving happens. Neither error-reports nor necessary results. The only error message I have is:

Error occurred during initialization of VM
agent library failed to init: instrument
Error opening zip file: C:\apache-tomcat-6.0.33\lib\wrong-jar.jar

in a case when I do a mistake in a aspectjweaver-1.6.1.jar name when specify a javaagent parameter. If it's written correctly - no error messages are printed.

Any ideas, what am I doing wrong?

P.S. I use Java 5, and I tried the same things with 1.5.4 version of the aspectj with exactly the same results.

2012-04-05 16:09
by Roman
Did you ever figure out this issue? I'm trying to configure Aspectj + tomcat with load time weaving in Eclipse. Specifically I'm trying to weave JSP's but am having no luck - bsimic 2012-08-10 16:50
I have a similar situation but have it working when the annotation is on my implementation class.Is your com.mypackages.MyClass an interface or implementation? My aspects were not getting weaved when I have the annotation on the interface. Also one more thing I have the aop.xml file under ../webapps/MyAppDeployed/WEB-INF/classes/META-INF/aop.xml not sure if that makes a difference - dineshr 2013-11-07 13:38


4

If you want to use load time weaving, you can first compile your classes with javac as usual then compile your aspect(s) with (i)ajc. You can do this with an ant task like below

<target name="compile-aspect">
    <iajc source="1.6" target="1.6" showweaveinfo="true" verbose="true" outxml="true" debug="true" outjar="${dist.dir}/myaspect.jar">
            <argfiles>
                    <pathelement location="${src.dir}/sources.lst"/>
            </argfiles>
            <classpath>
                    <path refid="master-classpath" />
            </classpath>
    </iajc>
</target>

It is enough to have aspectjrt.jar in the classpath ("master-classpath") during compilation.

Since all of my Java classes in ${src.dir}, I give a source list to iajc. In source list there is only one line.

sources.lst

com/xx/yy/zz/LoggingAspect.java

I set some of iajc task's attributes as follows

outxml="true"
outjar="jar_file_name"

When I run compile-aspect task I have a jar jar_file_name.jar file contains

META-INF/MANIFEST.MF
com/xx/yy/zz/LoggingAspect.class
META-INF/aop-ajc.xml

And finally add the *jar_file_name.jar* to your web application's WEB-INF/lib folder.

Then start Tomcat with -javaagents:/path_to_aspectjweaver.jar as you did before.

When I put the aop.xml (or aop-ajc.xml) in META-INF under the war file directly it doesn't work. This way (I mean seperating aspect classes into a jar) just works fine for me.

Hope this helps.

2012-09-30 22:39
by azizunsal
This solution worked for me. The point that I was missing and witch is obviously very important is the tag outxml="true" when building. Without this, you don't have the META-INF/aop-ajc.xml that the aspectjweaver uses in order to know what are the aspects to apply - Jean Bob 2015-11-25 16:36


4

I was trying to solve the same issue in Tomcat. In addition to -javaagent option, you need to make sure the aop.xml must be under the WEB-INF/classes/META-INF dir. This made the difference for me.

2014-11-12 23:15
by Naresh Dhiman
It also worked for m - Piyush Aghera 2015-07-27 09:06
Ads