General
Since the whole AOP programming method is new to me AND it took me a while to understand how to work with AspectJ and IntelliJ I've decided to create this brief tutorial on how to setup simple Hello World project using AspectJ and IntellJ.
The Ingredients
- IntelliJ Ultimate (I used version 14, you can get it from here: https://www.jetbrains.com/idea/download/)
- NOTICE: You must have the ultimate edition. I was not able to use AspectJ with community edition!
- I hope you know how to install IntellJ, basically it's next next next.
- AspectJ installation (I've used version 1.8.6, you can get the installation JAR here http://www.eclipse.org/downloads/download.php?file=/tools/aspectj/aspectj-1.8.6.jar)
- You need to install the jar by double clicking on it in Windows or just run the command from command line 'java -jar aspectj-1.8.6.jar'
- NOTICE: Remember where the aspect installtion path since we'll need it later. In windows the default is c:\ (My installation was to c:\aspectj1.8)
So, let's start...
Configuration
- Start the IntelliJ
- Click on Create new project
- Choose Java project
- Don't use template, just click next
- Name the project AspectJDemoHelloWorld
- Click Finish
- Open the settings (in windows Ctrl+Alt+S)
- Type compiler in the top search field, Chooe the Java Compiler. You should see something like this
- Click on Use compiler and choose Ajc (That is the AspetJ compiler)
- Click on the 'Path to Ajc compiler' browse button (near the Test button) and choose the 'aspectjtools.jar' at the lib directory of the AspectJ installation (In my case, C:\aspectj1.8\lib\aspectjtools.jar)
- Click on the Test button. You should see something like this
- In the search field, type plugins. You should something like this
- Click on the search field inside the plugins window and type AspectJ. Verify both plugins are enabled
- Press ok
- Open the project preferences (Ctrl + Alt + Shift + S in windows)
- Click on Modules in the left navigation pane and then click Dependencies.
- Click on the plus sign and choose JARs or directories.
- Go to the installation folder of the AspectJ and choose the file 'aspectjrt.jar'
- Press OK.
- That's for the configuration part
Coding
- Go to your src directory
- Create a new JAVA class named Hello, Paste in the following
- Create a new Aspect. Right click and choose Aspect (This ability was enabled due to adding of aspectjrt.jar)
- Name the Aspect World and click ok
- Paste the following in the aspect
- Click on Build --> Rebuild project
- Right click on Hello.java and choose Run
- You should see 'Hello World' in the System console
public class Hello { public static void main(String[] args) { sayHello(); } public static void sayHello() { System.out.print("Hello "); } }
public aspect World {
pointcut greeting() : execution (* Hello.sayHello(..)); after() returning() : greeting() { System.out.println("World"); } }
If you only see Hello, that means the Aspect code was not compiled meaning, you should check the Ajc compiler is the compiler chosen (see step 7 in the configuration part)