Message-ID: <143058732.298680.1368935019920.JavaMail.firstname.lastname@example.org> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_298679_1529708888.1368935019919" ------=_Part_298679_1529708888.1368935019919 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Author: jboner jboner= a>
In the previous tutorial we lear= ned how to write, define, weave in and run an aspect. So if you have not re= ad 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 a= nd after a specific action is triggered.
In this tutorial we will learn how to make use of one of the most powerf=
ul constructs in AOP; the around advice. We will implement a littl=
e around advice that hijacks the
o make the traditional greeting a bit more interesting.
Here is the Hello World application for this tutorial.
It is important to notice that the HelloWorld application is changed som=
e to make this tutorial more relevant.
The HelloWorld2.greet() method now returns a greeting message while in the = previous tutorial, the HelloWorld.greet() was simply printing out the messa= ge directly.
It is a standard Java application and can be compiled with
d target HelloWorld2.java.
However this application is indeed a bit boring and we are all pretty ti=
red of it. So let's try to make it a bit more interesting, let's hijack the=
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&quo= t; or "instead of" the the actual method invocation (can be any w= ell-defined action, aka join point), "intercepting" the = invocation. Basically it allows us to replace the regular code and define o= ur own code that runs instead of the regular code.
But the key point is that we also have the "magic" method
Let's take a look at some code to make less abstract:
In this code we have an advice method called
code>, which we define (using annotations) as being an around advi=
ce. What this method does is first invoke the original
method (which we have intercepted using the
advice) using the "magic" method
JoinPoint.proceed(). This method returns the result from the original method invocation. In t=
his case the
"Hello World!" string. So what we then =
do is making the Hello World application yell the greeting. (Well don't kno=
w if it was that much more fun.)
Around annotation we also bind the advice to a speci=
fic join point, in this case an anonymous one that picks out the
For more information about the annotation and XML definition, pointcut p= atterns etc. see the online documentation.
We also have to provide a tiny XML deployment descriptor (in this exampl=
aop.xml) to tell the AspectWerkz system which Java cl=
asses it should treat and load as aspects:
After adding annotations you need to run a special AspectWerkz tool. Thi=
s is done after compiling your aspect class files, (i.e. after running
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 <=
te compilation" class=3D"external-link" rel=3D"nofollow">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 t=
See the previous Hello World tutoria= l on the online documentation for details. <= /p>
This produces the expected output:
and as you can see, the greeting is now yelled out.
The key points we are trying to illustrate here is that:
HelloWorld2application 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 v= oid).
However, you might have noticed that the definition (in the aspect annot=
ation) is not generic but coupled to the
application. We can easily loosen up this strong coupling and make =
the aspect completely reusable. For more information about that, read the <=