What do we have in this session?
In this very short tutorial we are going to try a very simple example on Aspect oriented Programming (AOP). We will use Eclipse IDE to build a very simple Java program example that demonstrates AOP. First and foremost, please read and try to understand a simple AOP tutorial at AOP with Aspectj.
Download and Run Eclipse IDE
Then, download Eclipse from www.eclipse.org and choose Eclipse IDE for Java EE Developers (190 MB) package. Please read the 'installation' procedure document/manual as needed.
After downloading the eclipse-jee-galileo-SR2-win32.zip file, use 7 zip to unzip the compressed file (using Windows zip will generate error!).
Running Eclipse
Then run the eclipse executable.
If there is a workspace path prompt displayed, accept the default given or you can change it to your desired path.
Net, let try building and running the sample AOP using AspectJ. The example is a very simple Hello World application with two aspects: AspectJAspect and AspectJAnnotation. While AspectJ at first introduced a Java-like language to write aspects, with AspectJ version 5 it’s possible to write aspects as classes with special annotations and the two aspect examples demonstrate both approaches. You can download the Eclipse sample project as tar.gz or zip and thanks to the author; all copyright must goes to him.
Trying a Sample AOP Example
Next, we unzip the sample code.
The following are the source files and folders.
Then, we copy the sample code's project folder to the Eclipse's workspace folder. However, this can be done later, automatically.
Importing Sample AOP Project into Eclipse
Then, import the sample project to the current Eclipse workspace.
Expand the General folder > select Existing Projects into Workspace > click Next button.
Click Browse to select the sample project root directory.
If we skip the step on copying the sample project folder (copy projects into workspace check box) into the workspace as previously done, we can still copy the project into the workspace at this stage. Click Finish button.
Download and Update AspectJ Development Tools (ADJT)
If your Eclipse doesn’t have the AJDT, you need to download the 'plug-in' manually or through the Eclipse update. For Eclipse update, use the following steps.
Get the stable/release update link (in this case it is: http://www.eclipse.org/ajdt/downloads/) for the correct Eclipse version and in this case: http://download.eclipse.org/tools/ajdt/35/update/. Click Help menu > select Install New Software.
‘
Paste the update link, click the Add button and wait.
Then, select all the required and optional plug-ins. Click Next/Finish and wait until all the modules are downloaded and installed.
Build and Run the Sample AOP Project
Next, we are ready to build and run the simple program example. In Project Explorer, select AspectJTest project folder > right-click mouse > select Run As sub menu > select Java Application.
Select the Main (main project entry point). Click OK.
|
|
Then, let view as fast view. Click the small icon on the bottom-leftmost Eclipse IDE.
Select Navigator menu.
In Navigator window, double click AspectJAnnotation.java file.
The file content should be displayed in the right windows. Now, we have some error regarding the org.apache.log4j.Logger package. The package cannot be found so let tell Eclipse how to find the package.
Resolving the Java Package Error
Open the project property. Select the project folder > right-click mouse > select Properties menu.
Select Java Build Path link on the left > select Library page on the right window > click Add External JARs button.
Find %eclipse-jee-galileo-SR2-win32\eclipse\plugins path/folder > find and select org.apache.log4j.Logger_xxxversionxxx.jar file > click Open button. If you cannot find the package, you need to download it, for example, from: Apache's log4j.
The package should be visible in the Libraries page. Click OK button to dismiss the project property page.
Also, the log4j package should be visible in Project Explorer.
Re-run (re-build) the project example as done previously.
The output sample should be visible in the Console window.
Finally, close the project (close all the files).
Further Reading and Investigation
Aspect Oriented Programming tutorial with Spring can be found at: Aspect Oriented Programming with Spring