Java, Aspect Oriented Programming, Aspectj and Eclipse

 

 

 

 

 

 

What do we have in this session?

 

  1. Download and Run Eclipse IDE

  2. Running Eclipse

  3. Trying a Sample AOP Example

  4. Importing Sample AOP Project into Eclipse

  5. Download and Update AspectJ Development Tools (ADJT)

  6. Build and Run the Sample AOP Project

  7. Resolving the Java Package Error

  8. Further Reading and Investigation: AOP with Spring

 

 

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.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - download Eclipse IDE

 

After downloading the eclipse-jee-galileo-SR2-win32.zip file, use 7 zip to unzip the compressed file (using Windows zip will generate error!).

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - uncompress the Eclipse ZIP file

 

Running Eclipse

 

Then run the eclipse executable.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - the Eclipse executable file (and other files and folders)

 

If there is a workspace path prompt displayed, accept the default given or you can change it to your desired path.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - the Eclipse IDE

 

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.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - unzipping the AOP with AspectJ zip file

 

The following are the source files and folders.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - the unzipped 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.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - importing the existing Java project into Eclipse

Expand the General folder > select Existing Projects into Workspace > click Next button.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - selecting the existing project type

 

Click Browse to select the sample project root directory.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - selecting the existing project's 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.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - an option to copy the whole existing project into the Eclipse workspace

 

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.

 

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - download and install new software or modules or plug-ins to the Eclipse

 

Paste the update link, click the Add button and wait.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - selecting the update URL for AspectJ Development Tool

 

Then, select all the required and optional plug-ins. Click Next/Finish and wait until all the modules are downloaded and installed.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - AspectJ Development Tools (AJDT) components to be downloaded

 

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.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - running the project opened in Eclipse as java Application

 

Select the Main (main project entry point). Click OK.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - selecting the Main project entry point

 

 

Then, let view as fast view. Click the small icon on the bottom-leftmost Eclipse IDE.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - invoking more Eclipse features

 

Select Navigator menu.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - invoking the Navigator page

 

In Navigator window, double click AspectJAnnotation.java file.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - invoking the java source 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.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - errors in the Java source file for the missing log4j import

 

Resolving the Java Package Error

 

Open the project property. Select the project folder > right-click mouse > select Properties menu.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - invoking the project properties page in Eclipse

 

Select Java Build Path link on the left > select Library page on the right window > click Add External JARs button.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - adding the external JARs file into the Eclipse existing project

 

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.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - selecting hte org.apache.log4j_1.2.13.xx JAR file

 

The package should be visible in the Libraries page. Click OK button to dismiss the project property page.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - the new added JAR file is visible in the Eclipse Libraries page

 

Also, the log4j package should be visible in Project Explorer.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - the content of the JAR library file

 

Re-run (re-build) the project example as done previously.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - re-running the project in Eclipse as Java Application

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - selecting the project main entry point

 

The output sample should be visible in the Console window.

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - a sample project output seen in the Eclipse output window

 

Finally, close the project (close all the files).

 

Java, Aspect Oriented Programming, Aspectj and Eclipse - closing the project

 

Further Reading and Investigation

 

Aspect Oriented Programming tutorial with Spring can be found at: Aspect Oriented Programming with Spring

 


Back to Main