|
Helping ordinary people create extraordinary websites! |
Develop Aspect-Oriented Java Applications with Eclipse and AJDTBy Matt Chapman, Dr. Helen Hawkins2005-05-16
First steps Now that you've installed AJDT, let's explore how it supports the creation and running of AspectJ applications. To this end, we'll create a simple AspectJ application consisting of just one aspect and one class, and in so doing examine the AJDT features that we'll need to achieve this. The application is slightly contrived, but it serves our purpose. In it, we simply calculate the square of two numbers. At first, every time the program is given a number, it calculates the square from scratch -- and we've added a Thread.sleep() call within this method to ensure that this takes some time. By adding a cache aspect, however, we can check to see whether the result has already been calculated for a given input. If so, the cached value is returned; if not, then the square is calculated as normal and the result is added to the cache. Thus, the cache aspect will improve our application's performance. (For a discussion on using aspects for caching, please follow the Aspects Blog link in the Resources section.)If you've used the JDT Eclipse tools for Java development, then using AJDT for AspectJ development will be a familiar experience. To create a new AspectJ project, follow these steps:
Note: If this is the first AspectJ project you've created in your workspace, the AJDT Preferences Configuration wizard will appear after step 4. This wizard configures some Eclipse settings that will make your life easier. Accept the wizard's defaults and click Finish. Expand the project node in the Package Explorer and you'll see that the AspectJ runtime library aspectjrt.jar has been added in the same way that the JRE system library was. Now, select your project and go to File>New>Package to create a package called Now edit your class so that it looks like Listing 1. Listing 1. Main class for our application
From the code in Listing 1, you can see that in our class we have a Before adding our cache aspect, let's run the application. As with a Java application in a Java project, to run the application you need to select in the Package Explorer the Java class that contains the Now let's add an aspect to our application. The process of creating an aspect has the same look and feel as the process of creating a Java class. To create an aspect called Edit Cache.aj so that it contains the code in Listing 2. Listing 2. An aspect to add a cache to the our application
From the code in Listing 2, you can see that in our If you have Eclipse set to build projects automatically, all that's left is to run your AspectJ application! If this is not the case, select your AspectJ project in the Package Explorer and click the Build button, as shown in Figure 1. You're now ready to go. Since you've already run an earlier version of this application, you should have a run configuration all set up. Therefore, to run the AspectJ application, select Run button from the task bar (Figure 1), then select Application. You'll notice that it now takes much less time to calculate the squares the second time around! Figure 1. Build and run buttons Tutorial Pages: » Updated tools make AOP easier for beginners and veterans alike » Installing AJDT » First steps » The Outline view and editor markers » The Spacewar example » Build configurations » Aspect Visualization perspective » Debugging » Generating documentation » Further options » Conclusion » Resources First published by IBM DeveloperWorks |
|