Skip to end of metadata
Go to start of metadata

Authors: Alexandre Vasseur


This tutorial tries to walk you through the idea of Aspects deployment we have introduced in AspectWerkz.
This tutorial is using AspectWerkz 2.0.
You will learn how to integrate aspects and aspects libraries as regular components in your J2EE environment through the help of META-INF/aop.xml and WEB-INF/aop.xml files.

It won't give a lot of detail on what is AOP, how the aspects are implemented using AspectWerkz, etc...
If you want to read more about that first, you can read and practice the

In this tutorial, we will use Tomcat 5 samples applications since everyone can download it easily. Some more articles are on their way to explain integration on BEA WebLogic Server and Apache Geronimo, where the same concepts apply.


I assume you have a Java 1.4 correctly installed.

Download the 2.0 AspectWerkz release (or latest RC) and unzip it into a relevant location. I will use C:\aw\aspectwerkz\
This tutorial is based on the 2.0 version of AspectWerkz.

The latest distribution can be found here.

After installation you need to set the ASPECTWERKZ_HOME environment variable to point to the installation directory.
This is because quite a few of the scripts use this to find the required libraries and those scripts aims at making this tutorial
simpler to understand by focussing on the topic to cover.

Below is some samples (for Windows - adapt for Unix/Linux and do an export ASPECTWERKZ_HOME)
You should check that your JAVA_HOME is properly configured as well.

Then you need to install Tomcat 5, since we will deploy our aspects in Tomcat samples applications.
The distribution 5.0.25 can be found here (choose Download and unzip it to somewhere relevant.
I will use C:\aw\jakarta-tomcat-5.0.25\

After installation, it is wise to check the Tomcat installation by launching

and open your browser to (this link should work) http://localhost:8080/ to access the main Tomcat page.

In the next parts of the tutorial, we will go thru some Tomcat servlet samples http://localhost:8080/servlets-examples/ and jsp examples http://localhost:8080/jsp-examples/.

Shutdown Tomcat by closing the MSDos window or doing some CTRL-C twice.

You can check the attachments if you don't want to code or build the single class as explained in the following sections. I advise you to practice some instead of downloading them.

The reusable Aspect

Since Tomcat provides several sample applications, we have to provide a test Aspect that we can use and reuse to understand the different aspect deployment schemes.
To make things easy to understand, we will not go into the details.
Just notice that it is yet another tracing aspect, and that the aspect method named "trace" will be our around advice.

This aspect will print some information about himself, like the hashcode of the ClassLoader that created it (I do not use toString() since Tomcat is too verbose, but you can give it a try).

We will pack this Aspect in different ways in the next parts of the tutorial, but we can already compile it as a regular Java class.
I will write this Aspect in C:\aw\tomcat, in the package demoAOP

This is a standard Java class, and can be compiled with javac ... demoAOP\ to produce the regular ".class" file in the "target" directory. We need to link to the aspectwerkz jar due to this JoinPoint class that is used in the source code of the DemoAspect, so we should write
javac -classpath %ASPECTWERKZ_HOME%\lib\aspectwerkz-2.0.jar demoAOP\, but usage of the environment variable makes sense here to set the CLASSPATH:

From there we have several options if we are using online mode (class load time weaving), which is standardized in Java 1.5 with JSR-163 but that AspectWerkz support even for Java 1.3:

  • copy the aspect .class file in the WEB-INF/classes directory and write a WEB-INF/aop.xml AOP XML descriptor to define the aspect pointcut within the web application scope
  • package the aspect .class file in a jar file, and add a META-INF/aop.xml in this jar file. Copy this jar in the WEB-INF/lib directory.
  • instead of deploying our aspect within a specific web application, we could copy the jar at the system classpath level. The interesting thing to understand is that such a system wide META-INF/aop.xml AOP XML descriptor will affect all web applications due to java ClassLoader hierachies.
  • and of course we could have both: some aspects at the application level with a WEB-INF/aop.xml and some aspects at the system wide level with a META-INF/aop.xml

When using offline mode (post compilation of your classes before deployment), the same results can be achieved, though it can be sometimes a bit tricky to remember to weave the application both for its own aspects and for the system level aspects.
We will not go through offline mode in this tutorial (refer to HelloWorld tutorial).

To keep things simple, we will always use the following pointcut that will allow to pick out all public methods execution:

We carrfully exclude the package name of the aspect thus:

As a consequence, the following AOP XML descriptor will be used.
Write this file in C:\aw\tomcat\META-INF\aop.xml for now.

Note that we could have simplified this XML by defining our DemoAspect with annotations (see HelloWorld tutorial for this).

Some of you will have already understand that when the aspect is deployed within the web application, its weaving scope will not be the same: the
pointcut will pick out public method from classes really loaded by the web application (e.g. not javax.servlet.http.HttpServlet class).

Enabling online mode in Tomcat

Since we will use online mode, we need to alter some of the Tomcat startup scripts. If we were using BEA JRockit or Java 1.5, it would be a matter of adding a single JVM option.

Since I will not make any assumption on your JVM environment, I will skip the interesting details in this part and we will use a non-optimized but out-of-the-box solution that ships with the AspectWerkz distribution: the command line tool in ASPECTWERKZ_HOME/bin/aspectwerkz (read doc).

Note: for Tomcat 5.0.27 and after, please refer to the comments at the end of the article

Open the Tomcat startup script bin/catalina.bat (C:\aw\jakarta-tomcat-5.0.25\bin\catalina.bat).
The main idea is to set ASPECTWERKZ_HOME in this script and replace JAVA_HOME/bin/java with ASPECTWERKZ_HOME/bin/aspectwerkz.

Locate at the end of the script:

and alter it as follows:

For a complete understanding of the different options and other modes, refer to the AspectWerkz weaving schemes documentation.
Note that as a consequence of using the command line tool for simplicity here, we will run the Tomcat with -Xdebug JVM mode (with Java 1.4).

Do not startup Tomcat for now.

Aspect deployed in the application with a WEB-INF/aop.xml

This section explains the main step required and consequences of packaging an aop.xml file in your deployed application (WEB-INF/aop.xml).
The big picture is as follow:

In this section we decide to package our aspect as a regular class of the web application, and we define the pointcut and activate the aspect
through the help of the WEB-INF/aop.xml file.
As a consequence, only the classes of the web application will be eventually weaved (if the pointcut match), even if the pointcut is supposed to match everything (all public method execution as explained in the previous section). In fact, the deployment mechanism tied to the web application deployment add an unbreakable isolation, and only public method execution of classes loaded belonging to the web application will be targetted by our DemoAspect.

Lets apply our Aspect to the Tomcat jsp-examples web application:

Lets add the AOP XML descriptor to the web app:

To sum up we have only

  • added the aspect class to the web app as regular web app packaging dictates to do
  • added a WEB-INF/aop.xml file to the web app

Lets start Tomcat with the bin/startup.bat script (which will delegate to our modified catalina.bat script).
On the stdout window some AspectWerkz message should appear, with some verbose blocks of logs about aop.xml deployment.

And immediately we should see our DemoAspect at work, with some information about the web app initialization.

You will notice this 7088278 hashcode here, which indeed match the web app classloader hashcode as logged at deployment time.

By browsing the jsp-samples web application, we learn more about public method execution of the web application.
Off course, no information about Tomcat itself since the aspects was deployed within the web application.

This section is interesting, and I hope you have already some new ideas to deploy your aspects in your J2EE application with AspectWerkz.
But what happens if you would like to provide some ready to use out of the box aspects, completely obliouvness to the user ?

Shutdown Tomcat for the next section.

Aspect deployed in the application with an Aspect jar file

This section explains the main step required to package a "drop n weave" aspect jar file that contains a META-INF/aop.xml file, and consequence of adding this jar file in your deployed application (WEB-INF/lib).
The big picture is as follow:

In this section we will achieve the same result as in previous section, but in a slightly different way.
We will package our Aspect in a jar file, and we will add the aop.xml file in the jar file itself in the special META-INF folder, so that our jar file is completely self defined.
The jar file is then added to the web application as a regular jar file.
As a consequence of web application deployment, the aop.xml file from the aspect jar file will still be deployed by the web application class loader, and the same behavior will happen: the aspect will target classes laoded by the web application.

Lets prepare the jar file (regular java / jar packaging rules applies)

You will end up in having a C:\aw\aspect.jar file ready to use.

Lets add this drop n weave jar file to the Tomcat servlet samples web application.

Startup Tomcat.

Note: if you still have the jsp sample application with its aspect, you will see some messages as well as detailled in previous section.

The following should appear on stdout: some messages about the deployment of our aspect.jar file and its META-INF/aop.xml AOP XML deployment descriptor and right after that, our DemoAspect at work.

Something interesting: if you kept the jsp sample application aspect, you realize that you deployed the same aspect class and the same aop.xml file twice. This is made possible due to class loader hierarchies. They don't see each other and the naming space is guaranteed by the class loader and by the AspectWerkz AOP container.

Browse some in the servlet sample application and see the DemoAspect at work.

We will learn something more about that in the next part...
Have you heard that "Aspects are everywhere" (c) The AspectWerkz team ?
Go on reading !

Shutdown Tomcat

Aspect jar file at the system classpath

This section explains the main step required to package several META-INF/aop.xml files and understand what happens then.
The big picture is as follow:

In this section we keep the packaging of the previous section, but we add a new aspect.jar file at the Tomcat wide level (system classpath) to check if we can have there a more wider pointcut. Remember the pointcut is supposed to match all public method, but that AOP container behavior enforce isolation based on the regular class loader hierarchy.

There is an important things to do.
AspectWerkz AOP container requires sysem id as defined in the aop.xml file to be unique within a class loader hierarchy. That means:

  1. we can not just copy the previous aspect.jar at the system classpath level without changing its id
  2. we could just copy the previous aspect.jar to some other webapplication, since then it will be in a parallel classloader hierarchy due to regular J2EE classloading schemes

Lets change the META-INF/aop.xml then and may be lets reduce some the scope of the pointcut so that we don't have too much data:

The C:\aw\tomcat\META-INF\aop.xml file will look like

Lets package a new weave n drop aspect jar file:

You will end up in having a C:\aw\aspect-system.jar file ready to use.

Copy this file to C:\aw\jakarta-tomcat-5.0.25\common\lib which contains some regular jar for Tomcat.

Startup Tomcat.

Now we have both DemoAspect of each system at work:


We have:

  1. learned how to package and deploy our aspect along our web application
  2. understood the consequence of doing so
  3. understood a way to do it with a self contained jar file dropped in the web application libs
  4. understood the difference with an Aspect jar that would belong to the system classpath
  5. practice class load time integration using the command line tool

To go on in practicing, you can check that the AOP container will refuse deploying the same system id within the same class loader hierarchy, or you can find some interesting pointcut tied to Tomcat and Servlet 2.3 API, or try to change the Aspect to have it a bit more usefull.

  • No labels


  1. Share my experience on Tomcat5.0.27 and JBoss here.


    1. Open the setclasspath.bat (under %CATALINA_HOME%/bin directory) which is answer for setting CLASSPATH and Java options.
    2. Locate the script:
    set _RUNJAVA="%JAVA_HOME%\bin\java"
    3. Replace it with
    @REM ** Begin of AspectWerkz configuration **
    set ASPECTWERKZ_HOME=D:\documents\aspectwerkz\aspectwerkz-1.0-beta1\aspectwerkz3
    set _RUNJAVA=%ASPECTWERKZ_HOME%\bin\aspectwerkz.bat
    @REM ** we can add some more AspectWerkz option this way:
    @REM due to an open issue in beta1 version - this is just an optimization option
    @REM ** End of AspectWerkz configuration **

    JBoss3.2.4RC1 and JBoss4.0.0RC1

    1. Open the JBoss startup script bin/run.bat.
    2. Locate the script:
    set JAVA=%JAVA_HOME%\bin\java
    3. Replace it with
    @REM ** Begin of AspectWerkz configuration **
    set ASPECTWERKZ_HOME=D:\documents\aspectwerkz\aspectwerkz-1.0-beta1\aspectwerkz3
    set JAVA=%ASPECTWERKZ_HOME%\bin\aspectwerkz.bat
    @REM ** we can add some more AspectWerkz option this way:
    @REM due to an open issue in beta1 version - this is just an optimization option
    @REM ** End of AspectWerkz configuration **

  2. *******
    Note: please read
    set _RUNJAVA=%ASPECTWERKZ_HOME%\bin\aspectwerkz.bat
    instead of
    set _RUNJAVA=ASPECTWERKZ_HOME\bin\aspectwerkz.bat

    and similar in the JBoss part

  3. I have a problem!
    below is my envirment....
    tomcat 5.0.28
    aspectwerkz 2.0RC3

    I modify %TOMCAT_HOME%/bin/setclasspath.bat
    just like below

                  • before **************
                    set _RUNJAVA="%JAVA_HOME%\bin\java"
                        • after *********************

    set ASPECTWERKZ_HOME=C:\java\aspectwerkz-2.0.RC3
    set _RUNJAVA=%ASPECTWERKZ_HOME%\bin\aspectwerkz.bat
    set JAVA_OPTS=%JAVA_OPTS% -Daspectwerkz.transform.details=false


    after run %TOMCAT_HOME%/bin/startup.bat
    and i am tring to connect a servlet in the web application that i am testing.
    my PC's cpu utility rate is increase to 100% and can't return normal.
    is that something wrong!!
    Is there someone have the same problem???

    PS:i use the test aspet and put aop.xml to myweb/WEB-INF.

  4. Have you use AspectWerkz 2.0 to run on tomcat and running successfully?
    Could you share the configuration.
    thanks for help.

  5. Anonymous

    1.3 JVMs to not have the required XML parser(s) for AW. To remedy this, simply copy the
    xmlParserAPIs.jar and xercesImpl.jar from your $TOMCAT/common/endorsed/ directory
    into your jre/lib/ext/ directory. You could probably modify the aspectwerkz shell
    script to include these jars in its classpath, as well.

  6. I'm trying to get drop-n-weave to work with Weblogic 8.1 sp 4.

    I added ...
    set JAVA_HOME=C:\bea\jrockit81sp4_142_05
    set ASPECTWERKZ_HOME=C:\java\aspectwerkz-2.0
    set _EXECJAVA=%ASPECTWERKZ_HOME%\bin\aspectwerkz.bat
    ... to startWeblogic.cmd, as well as changed the java call to use...

    The server starts. I even got ...
    AW::WARNING - no such deployment model [perThread] using default (perJVM)
    ... because I'd used the wrong setting in my aop.xml.

    But the aspect doesn't seem to run.

    I'm trying to advise a control that has a data source, but I'm doing it in a domain that does not have the data source. This is on purpose because I'm trying to mock the control for testing and JPF development without the data source. (just take my word for it When my JPF calls the control I get an exception because the data source isn't found. Apparently the control (a local EJB) is still being initialized even though my aspect short-circuits the call...


    • @Around execution(*
      public Object mockSave(final JoinPoint joinPoint) throws Throwable
      Unknown macro: { MethodRtti rtti = (MethodRtti)joinPoint.getRtti(); Account account = (Account)rtti.getParameterValues()[0]; System.out.println("===> account}

    Is this normal? Or did I miss something?

    Most importantly, can I use an aspect to mock my control in a domain without the data source?


  7. Uh, sorry. I'm not sure how to post code. Let me try again...

    * @Around execution(*
    public Object mockSave(final JoinPoint joinPoint) throws Throwable {
    MethodRtti rtti = (MethodRtti)joinPoint.getRtti();
    AccountBizObject account = (AccountBizObject)rtti.getParameterValues()0;
    System.out.println("===> account: " + account);
    return new Long(1122);

  8. Still digging.
    I realized I'd made some mistakes. I was pointing the aspect to the interface instead of the class. I even had the wrong class! (doh!) then I realized the ant script wasn't really running AspectC.
    Now I'm pointing at the right class and I've set up the ant task AspectWerkzCTask. I can't seem to find any documentation on using the task though. Is this normal output? (Itried to escape the @s)...
    [awc] ******************************************************************
    [awc] * ClassLoader = sun.misc.Launcher$ExtClassLoader@e80a59@15207001
    [awc] * SystemID = virtual_15207001, 1 aspects.
    [awc] ******************************************************************
    [awc] ******************************************************************
    [awc] * ClassLoader = sun.misc.Launcher$AppClassLoader@53ba3d@5487165
    [awc] * SystemID = virtual_5487165, 1 aspects.
    [awc] ******************************************************************
    [awc] ******************************************************************
    [awc] * ClassLoader =
    [awc] * SystemID = virtual_18163514, 1 aspects.
    [awc] ******************************************************************
    [awc] ******************************************************************
    [awc] * ClassLoader =
    [awc] * SystemID = virtual_24595355, 1 aspects.
    [awc] ******************************************************************
    [awc] ******************************************************************
    [awc] * ClassLoader =
    [awc] * SystemID = virtual_24595355, 1 aspects.
    [awc] ******************************************************************
    [awc] ( 2 s )

    ... I feel so close, and yet so far.

  9. Anonymous

    Hi...I want to write an aspect for a method in which I need to manipulate with the parameters to that method.How can I achieve this..An urgent help needed. Thanks in advance.

  10. Anonymous

    Can anybody please update the steps for Tomcat as a service? No such files in my Tomcat (as service) install:


    So, how can I enable things in my setup? Thanks.


  11. Anonymous

    Hi Alexander , i am following the exact steps as yours , using the same versions for AspectWerkz (2.0 ) and Tomcat (5.0.25) as yours. but i never see the following lines in  the output :

    * ClassLoader = org.apache.catalina.loader.WebappClassLoader@7088278
    * SystemID = webapp
    * Aspect total count = 1
    * file:/C:/aw/tomcat/java/jakarta-tomcat-5.0.25/webapps/jsp-examples/WEB-INF/aop.xml

    when the aspects are deployed with the WEB-INF/aop.xml  , i get the log in the output

    but when the aspects are copied in the application as Aspect jar in WEB-INF/lib , i get no log ..
    Same problem happens when i copy the jar with to common/lib

  12. Hi,

    I'm trying to integrate AspectWerkz with tomcat 6.0.16. i followed the steps described in a preview comment but i received the exception bellow :

    GRAVE: Erreur lors du déploiement de l'archive jrm-web.war de l'application web
    java.lang.LinkageError: loader (instance of org/apache/catalina/loader/WebappCassLoader): attempted duplicate class definition for name: "org/apache/xerces/axp/SAXParserFactoryImpl"
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(
    at org.apache.catalina.loader.WebappClassLoader.findClassInternal(
    at org.apache.catalina.loader.WebappClassLoader.findClass(
    at org.apache.catalina.loader.WebappClassLoader.loadClass(
    at org.apache.catalina.loader.WebappClassLoader.loadClass(
    at javax.xml.parsers.FactoryFinder.getProviderClass(
    at javax.xml.parsers.FactoryFinder.newInstance(
    at javax.xml.parsers.FactoryFinder.findJarServiceProvider(FactoryFinderjava:294)
    at javax.xml.parsers.FactoryFinder.find(
    at javax.xml.parsers.SAXParserFactory.newInstance(SAXParserFactory.java128)
    at org.apache.tomcat.util.digester.Digester.getFactory(
    at org.apache.tomcat.util.digester.Digester.getParser(
    at org.apache.tomcat.util.digester.Digester.getXMLReader(
    at org.apache.tomcat.util.digester.Digester.parse(
    at org.apache.catalina.startup.TldConfig.tldScanStream(
    at org.apache.catalina.startup.TldConfig.tldScanJar(
    at org.apache.catalina.startup.TldConfig.execute(
    at org.apache.catalina.core.StandardContext.processTlds(StandardContextjava:4441)
    at org.apache.catalina.core.StandardContext.start(
    at org.apache.catalina.core.ContainerBase.addChildInternal(
    at org.apache.catalina.core.ContainerBase.addChild(
    at org.apache.catalina.core.StandardHost.addChild(
    at org.apache.catalina.startup.HostConfig.deployWAR(
    at org.apache.catalina.startup.HostConfig.deployWARs(
    at org.apache.catalina.startup.HostConfig.deployApps(
    at org.apache.catalina.startup.HostConfig.start(
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.jav:311)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(
    at org.apache.catalina.core.ContainerBase.start(
    at org.apache.catalina.core.StandardHost.start(
    at org.apache.catalina.core.ContainerBase.start(
    at org.apache.catalina.core.StandardEngine.start(
    at org.apache.catalina.core.StandardService.start(
    at org.apache.catalina.core.StandardServer.start(
    at org.apache.catalina.startup.Catalina.start(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpljava:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at org.apache.catalina.startup.Bootstrap.start(
    at org.apache.catalina.startup.Bootstrap.main(
    2009-01-29 09:34:43 org.apache.catalina.core.StandardContext processTlds

    can anyone help me please.