This tutorial does not explain AOP, however if your new to the idea of AOP, then please check out JavaWorld's series of articles to get you started.
Download the latest release and unzip it into the relevant location. This tutorial is based on 0.10.
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. How this variable is set depends on you OS. Since I'm using Linux I've amended my
.bashrc file, windows users could do this by using the control panel.
The Test Application
Now we've installed aspectwerkz, we need a test application into which to weave our aspects. As is the tradition, I'm going to use the standard HelloWorld application.
This is simply a standard Java application, and can be compiled with
javac -d target HelloWorld.java
Writing the Aspect
Next we need to develop the aspect which will contain the code to be weaved into our HelloWorld class. In this example I'm going to output a statement before and after the greet method is called.
Notice the signature of the aspect methods. They need to take this
JoinPoint argument otherwise the AspectWerkz weaver won't be able to identify the method when the aspect is weaved in, (and can leave you scratching your head as to why the weaving isn't working!).
To compile this aspect class you'll need to include the
aspectwerkz-0.10.jar in the classpath, i.e.
javac -d target -classpath $ASPECTWERKZ_HOME/lib/aspectwerkz-0.10.jar MyAspect.java
Defining the Aspect
At this point we have the test application and the actual aspect code, but we still need to tell AspectWerkz where to insert the aspect methods (pointcuts), and which aspect method to actual insert (advice).
Specifying pointcuts and advice can be done using either of (or a mixture of), the following methods.
Using an XML definition file
The XML definition file is just that, an XML file which specifies the pointcuts and advice using XML syntax. Here's one that will weave our MyAspect class into our HelloWorld program (
Most of this should be pretty straight forward, the main part being the aspect tag. Whilst I'm not going to explain every bit of this definition file, (I'll leave that up to the official documentation), I will explain a few important points.
When specifying the
pointcut the name can be any label you like, it's only used to bind the
advice. The expression should be any valid expression occording to the Join point selection pattern language however you MUST make sure that the full package+class name is included in the pattern. If this isn't done, or if the pattern is slightly wrong, AspectWerkz won't be able to correctly identify the 'greet method'.
advice tag, the
name attribute should be the name of the method in the aspect class, (specified in the
aspect tag), which you wish to insert at the specific joinpoint. Type is set to
around, depending on where exactly you wish to inser the method in relation to the joinpoint.
bind-to specifies the name of the
pointcut to which this
advice will be bound.
This example identifies the
HelloWorld.greet() method and assigns it the pointcut label "greetMethod". It then inserts the
MyAspect.beforeMethod method just before
greet() is called, and
MyAspect.afterMethod() just after the
greet() method returns.