IntelliJ IDEA 2019.2 Help

AspectJ

AspectJ support in IntelliJ IDEA Ultimate is enabled by default and includes the following features:

  • Ability to create aspects in two forms: as .aj files and java files containing classes annotated with @Aspect.

  • Coding assistance (including code completion) when writing aspect code. For annotation-style aspects, coding assistance is provided in full; for code-style aspects, assistance is limited to inter-type declarations.

  • Ability to perform basic aspect refactorings such as Rename and Move, and also the Push ITDs In refactoring for inter-type declarations.

  • Integration with the AcpectJ compiler ajc, which you can run right from the IDE. This compiler is part of the AspectJ distribution which you can download from the AspectJ website.

  • Ability to configure ajc at the project level with an option of fine-tuning its use at the level of individual modules.

Enable the AspectJ plugins if necessary

The bundled AspectJ and Spring AOP/@AspectJ plugins should be enabled by default. If they are disabled, do the following:

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), select Plugins.

  2. Click the Installed tab and search for aspectj.

  3. Enable the bundled AspectJ and Spring AOP/@AspectJ plugins.

Add a library with the AspectJ runtime to your project

Download and install AspectJ as described on the Downloads page. The AspectJ runtime aspectjrt.jar should be located in the installation directory under /lib.

  1. Open the Project Structure dialog Ctrl+Shift+Alt+S and select Libraries.

  2. Click The New Project Library button and select Java.

  3. Specify the location of aspectjrt.jar in the file system.

  4. Select the modules to which this library should be added and click OK.

Add AspectJ dependencies

  1. Open the Project Structure dialog Ctrl+Shift+Alt+S and select Facets.

  2. Click The Add button and select AspectJ.

  3. Select the modules for which you want to use this facet and click OK.

  4. Select the new AspectJ facet for the relevant module and add dependencies as paths to corresponding aspects.

Create an aspect

After you add the necessary AspectJ library and dependencies, you can start developing your aspects.

  1. In the Project tool window, select the directory where you want to create the aspect, press Alt+Insert, and select Aspect.

  2. Specify the name and type of the aspect file, and click OK.

    • Aspect creates an .aj file with the specified name.

    • @Aspect creates a .java file containing a class annotated with @Aspect.

If necessary, you can refactor the source code of your aspects.

To compile your aspects using the AspectJ compiler ajc, select it as the project compiler. ajc is in aspectjtools.jar, which is located in the installation directory under /lib.

Last modified: 20 September 2019