Creating your first AspectJ project
In this section you will create a new AspectJ project and add a package and a Java class to your project.
1. Inside Eclipse select the menu item File > New > Project.... to open the New Project wizard.
2. Select AspectJ Project then click Next. On the next page, type "Hello World" in the Project name field and click Finish.
The Java perspective opens inside the workbench with the new AspectJ project in the Package Explorer.
3. Select the "Hello World" project in the package explorer then select File > New > Package.... to open the New Java Package wizard. Type "helloworld" in the name field and click finish.
4. Select the package that you just created in the package explorer then select File > New > Class.... to open the New Java Class wizard. Type "Hello" in the name field, select the option to allow Eclipse to create a main method and click finish.
5. Type the following into your new class.
public static void main(String[] args) {
sayHello();
}
public static void sayHello() {
System.out.print("Hello");
}
6. Save the file.
Creating an aspect
In this section you will create a new aspect and add a pointcut and some advice.
1. In the Package Explorer view, select the helloworld package. From the package's context menu, select New > Aspect.
2. Make sure that Hello World appears in the Source Folder field and that helloworld appears in the Package field. In the Name field, type World.
3. Click Finish to create the new aspect.
4. The new file is opened in the editor. It contains the new aspect, the constructor and comments.
5. Change the body of the aspect to the following:
public aspect World {
pointcut greeting() : execution(* Hello.sayHello(..));
after() returning() : greeting() {
System.out.println(" World!");
}
}
6. Save the file.
Running your programs
In this section, you will learn more about running AspectJ programs in the workbench.
1. Right click on Hello.java in the Package Explorer and select Java Application from the cascading Run menu. This will launch the selected class as a local Java application. Note that unless you need to run a main method that is in an aspect or use an aspectpath you do not need to use the Run > AspectJ/Java Application option.
2. Notice that the program has finished running and the following message has appeared in the console:



