Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

Authors: Alexandre Vasseur

This page is under construction - it will be ready before July 14th with 1.0 beta 1 release


This tutorial tries to walk you through the idea of Aspects deployment we have introduced in AspectWerkz 1.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.


Download the 1.0 AspectWerkz release and unzip it into a relevant location. I will use C:\aw\aspectwerkz
This tutorial is based on the 1.0 beta1 version of AspectWerkz (you need to move file after unziping since this beta release contains some extra aspectwerkz3 subdirectory etc.).

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.

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-1.0-beta1.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:

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).

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

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.

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.

Aspect deployed in the application with an Aspect jar file

Aspect jar file at the system classpath


Now we have:

  1. learned how to package and deploy our aspect along our web application
  2. understood the consequence of doing so
  3. understood the difference with an Aspect jar that would belong to the system classpath
  4. practice class load time integration using the command line tool
  • No labels