Author: jboner jboner
In the previous tutorial we learned how to write, define, weave in and run an aspect. So if you have not read it yet, please do that first.
We also learned how to write and use before and after advice, which allowed us to define code blocks that does something before and after a specific action is triggered.
In this tutorial we will learn how to make use of one of the most powerful constructs in AOP; the around advice. We will implement a little around advice that hijacks the
HelloWorld.greet() to make the traditional greeting a bit more interesting.
The Test Application
Here is the same old Hello World application.
It is a standard Java application and can be compiled with
javac -d target HelloWorld.java.
However this application is indeed a bit boring and we are all pretty tired of it. So let's try to make it a bit more interesting, let's hijack the
Writing and defining the Aspect
To be able to hijack the
greet() method we need to make use of the around advice construct.
The around advice can be seen as being invoked "around" or "instead of" the the actual method invocation (can be any well-defined action, aka join point), "intercepting" the invocation. Basically it allows us to replace the regular code and define our own code that runs instead of the regular code.
But the key point is that we also have the "magic" method
proceed() which allows us to proceed with the invocation and trigger the action that we have just replaced.
Let's take a look at some code to make less abstract:
In this code we have an advice method called
yellifyier, which we define (using annotations) as being an around advice. What this method does is first invoke the original
greet() method (which we have intercepted using the
yellifyier around advice) using the "magic" method
JoinPoint.proceed(). This method returns the result from the original method invocation. In this case the
"Hello World!" string. So what we then do is making the Hello World application yell the greeting. (Well don't know if it was that much more fun.)
Around annotation we also bind the advice to a specific join point, in this case an anonymous one that picks out the
For more information about the annotation and XML definition, pointcut patterns etc. see the online documentation.
We also have to provide a tiny XML deployment descriptor (in this example called
aop.xml) to tell the AspectWerkz system which Java classes it should treat and load as aspects:
Weaving and running the Aspect
After adding annotations you need to run a special AspectWerkz tool. This is done after compiling your aspect class files, (i.e. after running
javac). The tool, known as the
AnnotationC compiler, can be invoked as follows, passing in the source directory (
.), and the class directory (
For AspectWerkz 1.0 final:
More information on the
AnnotationC compiler can be found here.
Now we are ready to run the weave in our aspect and run the Hello World application. This is done using the
aspectwerkz command line tool:
This produces the expected output:
and as you can see, the greeting is now yelled out.
The key points I am trying to illustrate here is that:
HelloWorldapplication is completely oblivious of the fact that it is actually yelling out the greeting
yellifieradvice implementation is generic and can be used to yell out output captured from any method (that does not return void).
However, you might have noticed that the definition (in the aspect annotation) is not generic but coupled to the
HelloWorld application. We can easily loosen up this strong coupling and make the aspect completely reusable. For more information about that, read the next tutorial.