JBoss-IDE AOP Tutorial
Navigation
-
Tutorial Part 1
Synopsis
-
This tutorial is meant to guide you through creating a new AOP project in eclipse using the AOP extension to JBossIDE. It assumes that you have some working
knowledge of AOP, and Java.. and possibly some minimal experience dealing with eclipse as well.
Installation
-
For installation instructions, please see Installing JBossIDE/AOP
Creating a new AOP Project
-
From eclipse's main menu, you can click on the File Menu, and under it, New > Project...
Double click on JBoss AOP Project under the JBossAOP folder
In the Project Name text box, let's enter "HelloAOP" (without quotes -- make sure not to enter a space).
"Use Default" should be fine for the project location. If you want to use an external location, make sure there are no spaces in the path.
Click "Finish"
At this point, your eclipse workbench should look something like this:
Hello AOP
-
Now that the HelloAOP project is finished being setup, we can create a simple class and intercept it.
First off, let's create the class:
Right click on the "src" directory in the Package Explorer and in the menu, click New > Class.
The only thing you should need to change is the Name of the class. Enter "HelloAOP" without quotes into the Name textbox, and click "Finish"
At this point we'll want to make 2 methods.
public void callMe ()
An instance method that we can call and intercept. Inside we'll print "AOP!"
public static void main (String args{FOOTNOTE DEF })
The entry point into our AOP application (just like all normal java applications)
Here's the code:
public class HelloAOP { public void callMe () { System.out.println("AOP!"); } public static void main (String args[]) { new HelloAOP().callMe(); } }
Now for the fun part! Let's create and apply an Interceptor.
Creating the Interceptor
Create a new class called HelloAOPInterceptor. This class will implement the org.jboss.aop.advice.Interceptor interface. Make sure the "Inherited Abstract methods" box is checked before you click "Finish". Here's a screenshot of what the dialog should look like:
Once the Interceptor class is opened, you'll notice that two empty method stubs have been created for you.
public String getName()
this method returns what name the AOP framework will use internally for your Interceptor. In practice, it's probably best to return the name of the class.
public Object invoke(Invocation arg0) throws Throwable
this method does the actual interception. the argument name "arg0" isn't very helpful; I always rename it to "invocation". The sole responsibility of this method is to return the next link in the invocation chain. WARNING: if your method returns null, it will break the invocation chain, and any interceptors scheduled after yours will not be ran. It will also prevent the intercepted method (or field) from happening. It is expected that your invoke method will return invocation.invokeNext(). Any code that happens before that will be the body of your interceptor. In our Interceptor, we want to print "Hello, " (without a newline).
Here's the code:
import org.jboss.aop.advice.Interceptor; import org.jboss.aop.joinpoint.Invocation; public class HelloAOPInterceptor implements Interceptor { /* (non-Javadoc) * @see org.jboss.aop.advice.Interceptor#getName() */ public String getName() { return "HelloAOPInterceptor"; } /* (non-Javadoc) * @see org.jboss.aop.advice.Interceptor#invoke(org.jboss.aop.joinpoint.Invocation) */ public Object invoke(Invocation invocation) throws Throwable { System.out.print("Hello, "); return invocation.invokeNext(); } }
Applying the Interceptor
In order to apply your Interceptor to the callMe() method, we'll first need to switch back to the HelloAOP.java editor. Once the editor is active, you should be able to see the callMe() method in the Outline view (See figure).
Right click on this method, and click JBoss AOP > Apply Interceptor(s)...
A dialog should open, with a list of available Interceptors. Click on HelloAOPInterceptor, and click "Finish".
You should see in your Package Explorer that the file "jboss-aop.xml" now exists under your project root.
Running Hello AOP
Now all that's left is running the application!
Similar to running a normal Java Application from Eclipse, you must create a Run Configuration for your project.
From the Run menu of eclipse, and choose "Run..."
In the dialog that opens, you should see a few choices in a list on the left. Double click on "JBoss AOP Application".
Once it is finished loading, you should have a new Run Configuration under JBoss AOP Application called "Hello AOP".
Click the "Run" button
The console should now say: "Hello, AOP!"
Comments