Configuring Spring facet

Spring support is available in the Ultimate edition only.

To be able to use Spring in you project, you need to have a Spring facet that comprises libraries and UI elements for configuring Spring-specific settings.
IntelliJ IDEA can automatically detect Spring configuration in your code. It will inform you about missing configuration and will suggest necessary actions.

If you are creating a brand new project or module, you can use the wizard to select necessary frameworks and libraries.

If for some reason IntelliJ IDEA could not detect configuration files, you can add the facet manually:

  1. From the main menu, select File | Project Structure, or press Ctrl+Shift+Alt+S.
  2. From the left-hand list, select Modules.
  3. Select the necessary module and click images/add.png in the middle section.
  4. Select Spring from the list.
  5. You might need to set up a library when adding a facet. In this case, click Fix at the bottom of the window next to a warning message.
    If you already have a Spring library, you can use it as is, or create a new library using JAR files on your computer. In this case, select the Use library option.
    If you do not have a library, select Download.

If the Spring facet is configured correctly, you will see the gutter icons marking each component, bean, etc. To configure the gutter icons, press Ctrl+Alt+S and go to Editor | General | Gutter Icons.

Creating application context

Spring application context is a way of grouping configuration files in IntelliJ IDEA. When you create a context, you let IntelliJ IDEA understand relationships between configuration files included into this context.
You can create as many application contexts as you need; any configuration file may be included in more than one context.

In some cases, you will not need to configure a context. For example, Spring MVC  web applications have strict rules about their configuration. Spring support in IntelliJ IDEA can deduce them and create an autodetected application context for you, as well as set up the Web facet.

To create an application context:

  1. Navigate to File | Project Structure | Facets.
  2. Select the necessary Spring facet from the list and click images/add.png in the right-hand section.
  3. In the New Application Context dialog, enter a name, and select files you want to include in the context.
Use the images/delete.png and images/edit.png options to remove and edit contexts.

images/spring_add_context.png

Configuring parent context

IntelliJ IDEA allows you to configure a parent-child relationship between contexts.
Beans from a parent context are visible to beans in child contexts, but not vice versa. This way beans from child contexts can use configuration from the parent context.

For example, Spring MVC applications usually have two contexts. One context belongs to web layer beans, the other context is used for services and repositories. The web layer context will be a child context in this case, as you need to inject services into controllers, not other way around.

IntelliJ IDEA can configure parent context automatically. For example, if the IDE detects Spring Cloud context, it will make it a parent application context for Spring Boot.

To configure a parent context, use the New Application Context dialog.

The Multiple Context panel shows up on top of the editor for files included into two or more application contexts. You can use this panel to select another active context, for example, if you want to run your application with different configuration, and change highlighting. Click on the context name and select a context from the popup dialog.
To disable the panel, click images/settings.png, and deselect the Show Multiple Context panel checkbox.

Navigating Spring dependencies

Viewing Spring dependencies on diagram

The Spring dependencies diagram lets you view and analyze dependencies between beans and dependencies between configuration files in your project.
To build a diagram:

The UML Support plugin must be enabled to work with diagrams.

  1. In the project structure, right-click a configuration file for which you want to build a diagram.
  2. Select Diagrams from the menu.
  3. Select Show Diagram Popup (Ctrl+Alt+U) to open a diagram in a local popup window, or Show Diagram (Ctrl+Shift+Alt+U) to open the diagram in the editor.
  4. Select a diagram type. Use the Spring diagram to view beans that were defined in Spring configuration files.
    The Spring Model Dependencies diagram is used for viewing relationship between configuration files.

Bean names are considered symbols, so you can use the navigate to symbol option as well.
In Spring MVC you can also use URI request mapping to navigate to symbol. This will get you to the controller method that is mapped to the URI.

Different color edges on the diagram denote different types of connections between components (import, component scan, etc.). Tooltips on the nodes and the edges can give you details on what each element contributes to the application context.

images/spring_diagram_example.png

Using diagrams to detect setup errors

Errors in your Spring configuration can happen at the dependencies level. For example, a circular dependency.
Circular dependencies may be hard to detect as they do not cause any obvious errors. However, some beans will be loaded multiple times possibly resulting in unexpected behaviour.
On diagrams, red arrows mark errors, such as circular dependencies, and allow you to easily spot and remove them from your Spring configuration. images/spring_dep_cycle.png

Browsing dependencies in Spring tool window

The Spring tool window helps you navigate between Spring components and dependencies. You can view definitions for the Spring beans used in your project, and see how they are related to other beans.

To access the Spring tool window, navigate to View | Tool Windows | Spring.

For Spring MVC, the tool window lets you navigate to request mappings.
If your configuration comprises Spring Data, the Data tab of the tool window will show you the list of repositories, their queries and their projection.

The left part of the window lists all modules in your project.
Click on the necessary module to view application contexts, and then configuration files in these contexts.
For each configuration file, you can view the list of beans. By clicking on a bean, you can view its documentation and diagram showing its relation to other beans.
images/spring_toolwindow.png

Changing active profiles

Spring allows you to map specific contexts or beans to different profiles — for example, test or production. This way, you can activate different profiles in different environments.
If you have defined at least one profile in your project, IntelliJ IDEA will show a special panel on top of the editor. You can use this panel to view the current profile name and to change active profiles:

You can also change active profiles in the Spring tool window. Right-click a component and select Change Active Spring Profiles.

  1. Click Change Profiles on the panel.
  2. Select a component to which you want to map the profile. This can be either entire project, current module or current context.
  3. Select a profile to which you want to map this component.

images/spring_change_profiles.png If you want to hide the panel, click images/settings.png it. In the next dialog, deselect the Show Profiles panel checkbox.

Spring Boot

Getting started with Spring Boot

Spring Initializr is a wizard that allows you to select the necessary configuration when you are creating a project or a module. For example, you can select the necessary building tool, or add Spring Boot starters and dependencies.

To access the wizard, navigate to File | New | Project or Module, and select Spring Initializr. Follow the steps of the wizard to select technologies and dependencies you want to use.

Configuring custom configuration files

Some custom configuration files are configured automatically. For example, profile-specific configuration files with names that match the current naming pattern will be added to the context.

Spring Initializr creates one default configuration file that may not always be sufficient for your purposes. If you do not want to use the default configuration file, or if you want to run your code in different environments, you can use custom configuration files.
To do so, you have to let IntelliJ IDEA know which files are configuration files in you project. This will enable relevant highlighting and coding assistance.

  1. Navigate to File | Project Structure | Facets.
  2. Click images/spring_boot_icon.png (Customize Spring Boot) in the toolbar.
  3. If you want to use a custom configuration file instead of the default one, type in the name of a new custom configuration file in the search box.
    If you want to use multiple configuration files, click images/add.png and select files from the project tree.
  4. Click OK and apply the changes.
images/spring_boot_custom_config.png If a custom configuration file is set up correctly, it will be marked with the images/spring_boot_icon.png icon.

Running and monitoring Spring Boot applications

The endpoints feature is available in Spring Boot version 1.3 and later.

Spring Boot has built-in features that allow you to get key metrics and monitor the state of your application in production environment by invoking different endpoints, such as health or bean details.
In IntelliJ IDEA, you can view endpoints on the Endpoints tab. This tab appears on the Run dashboard, or in the Run/Debug tool window, when you run an application.

To enable the Run dashboard, open the Run/Debug Configurations dialog, and select Defaults. Under the Run Dashboard Types section, click images/add.png and select Spring Boot.

To show or hide the dashboard, go to View | Tool Windows and click Run dashboard.

The Run dashboard allows you manage your applications and monitor their state. If any errors are detected at runtime, the dashboard will display information that will help you identify and resolve them.

The Endpoints tab becomes available if the Enable JMX agent checkbox is selected in the Run/Debug Configurations dialog.
Moreover, make sure to add the org.springframework.boot.spring-boot-starter-actuator dependency to your module before running your application. This will let IntelliJ IDEA access and display the endpoints. For the mappings endpoint, add the org.springframework.boot.spring-boot-starter-web starter as well. images/spring_boot_endpoints.png