Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The Test Application

Here is the same old Hello World application for this tutorial.

It is important to notice that the HelloWorld application is changed some 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 message directly.

Code Block
java
java
package testAOP;

public class HelloWorldHelloWorld2 {

     public static void main(String args[]) {
         HelloWorld world = new HelloWorld();
         System.out.println(world.greet());
     }

     public String greet() {
         return "Hello World!";
     }
}

It is a standard Java application and can be compiled with javac -d target HelloWorldHelloWorld2.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 HelloWorldHelloWorld2.greet() method.

Writing and defining the Aspect

...

Code Block
java
java
package testAOP;

import org.codehaus.aspectwerkz.joinpoint.JoinPoint;

public class HelloWorldHijacker {

  /**
    * @Around execution(* testAOP.HelloWorldHelloWorld2.greet(..))
    */
    public Object yellifyier(JoinPoint joinPoint) {
        Object greeting = joinPoint.proceed();
        return "<yell>" + greeting + "</yell>";
    }
}

...

Code Block
$ASPECTWERKZ_HOME/bin/aspectwerkz -Daspectwerkz.definition.file=aop.xml -cp target testAOP.HelloWorldHelloWorld2

See the previous Hello World tutorial on the online documentation for details.

...

and as you can see, the greeting is now yelled out.

Conclusion

The key points I am we are trying to illustrate here is that:

  1. the HelloWorld HelloWorld2 application is completely oblivious of the fact that it is actually yelling out the greeting
  2. the yellifier advice implementation is generic and can be used to yell out output captured from any method (that does not return void).

...