IntelliJ IDEA 2016.3 Help

Context and Dependency Injection (CDI)

This feature is supported in the Ultimate edition only.

Basics

The Context and Dependency Injection (CDI) support in IntelliJ IDEA is based on the Java EE: Context and Dependency Injection plugin. This plugin is bundled with the IDE and enabled by default.

You can enable CDI support when creating a project or module. You can as well add CDI support for an existing module.

CDI in a module is represented by the corresponding CDI library in the module dependencies. You can download all the necessary library files right from within the IDE. You can also change the CDI version being used if and when needed.

Making sure that the CDI Support plugin is enabled

To make sure that the CDI Support plugin is enabled, follow these steps:

  1. Open the Settings dialog (e.g. Ctrl+Alt+S).
  2. In the left-hand part of the dialog, select Plugins.
  3. In the right-hand part of the dialog, on the Plugins page, type cd in the search box. As a result, only the plugins whose names and descriptions contain cd are shown in the list of plugins.
  4. If the check box to the right of Java EE: Context and Dependency Injection is not selected, select it.
  5. Click OK in the Settings dialog.
  6. If suggested, restart IntelliJ IDEA.

Enabling CDI support when creating a project or module

To enable CDI support, follow these steps:

  1. Do one of the following:
    • If you are going to create a new project: click Create New Project on the Welcome screen or select File | New | Project.

      As a result, the New Project wizard opens.

    • If you are going to add a module to an existing project: open the project you want to add a module to, and select File | New | Module.

      As a result, the New Module wizard opens.

  2. On the first page of the wizard, in the left-hand pane, select Java Enterprise. In the right-hand part of the page, specify the JDK to be used and select the Java EE version to be supported.
  3. Under Additional Libraries and Frameworks, select the CDI: Context and Dependency Injection check box.
  4. You'll need a library that implements CDI. You can choose to use an existing library, create and use a new one, download the library files if they are not yet available on your computer, or postpone setting up the library until a later time.
    • Use library. Select the library to be used from the list (if the corresponding library is already defined in IntelliJ IDEA).

      Create. If the corresponding library files (.jar) are already available on your computer, you can arrange those files in a library and use that new library. To do that, click Create and select the necessary files in the dialog that opens. (Use the Ctrl key for multiple selections.)

      Optionally, click Configure to edit the selected library. (For an existing library the Edit Library dialog will open, for the library that you have just created - the Create Library dialog.)

    • Download. Select this option to download the library files that implement CDI. (The downloaded files will be arranged in a library.)

      Optionally, click Configure to edit the library settings and contents. (The Downloading Options dialog will open.)

    • Set up library later. Select this option to postpone setting up the library until a later time.

    Configure. Click this button to edit the settings for the library selected next to Use library or the one that is about to be downloaded.

    Click Next.

  5. Specify the name and location settings. For more information, see Project Name and Location or Module Name and Location.

    Click Finish.

Adding CDI support for an existing module

To add CDI support for an existing module

  1. Open the Project tool window (e.g. View | Tool Windows | Project).
  2. Right-click the module of interest and select Add Framework Support.
  3. In the left-hand pane of the Add Frameworks Support dialog that opens, select the CDI: Context and Dependency Injection check box.
  4. You'll need a library that implements CDI. You can choose to use an existing library, create and use a new one, download the library files if they are not yet available on your computer, or postpone setting up the library until a later time.
    • Use library. Select the library to be used from the list (if the corresponding library is already defined in IntelliJ IDEA).

      Create. If the corresponding library files (.jar) are already available on your computer, you can arrange those files in a library and use that new library. To do that, click Create and select the necessary files in the dialog that opens. (Use the Ctrl key for multiple selections.)

      Optionally, click Configure to edit the selected library. (For an existing library the Edit Library dialog will open, for the library that you have just created - the Create Library dialog.)

    • Download. Select this option to download the library files that implement CDI. (The downloaded files will be arranged in a library.)

      Optionally, click Configure to edit the library settings and contents. (The Downloading Options dialog will open.)

    • Set up library later. Select this option to postpone setting up the library until a later time.

    Configure. Click this button to edit the settings for the library selected next to Use library or the one that is about to be downloaded.

  5. Click OK in the Add Frameworks Support dialog.

As a result, the CDI library you have specified is added to the list of module dependencies.

Changing the CDI version

T change the CDI version

  1. Open the Project Structure dialog (e.g. Ctrl+Shift+Alt+S).
  2. Select the CDI library whose version you want to change.
  3. Click Change Version. (If you are on the Dependencies tab, select to edit the library first.)
  4. In the Downloading Options dialog that opens, select the necessary CDI version, specify other settings as needed and click OK.

See Also

```

Reference:

Last modified: 21 March 2017