Configuring Spring facet
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 for some reason IntelliJ IDEA could not detect configuration files, you can add the facet manually:
- From the main menu, select , or press Ctrl+Shift+Alt+S.
- From the left-hand list, select Modules.
- Select the necessary module and click in the middle section.
- Select Spring from the list.
- 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.
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.
To create an application context:
- Navigate to .
- Select the necessary Spring facet from the list and click in the right-hand section.
- In the New Application Context dialog, enter a name, and select files you want to include in the context.
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.
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 , 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:
- In the project structure, right-click a configuration file for which you want to build a diagram.
- Select Diagrams from the menu.
- 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.
- 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.
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.
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.
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.
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.
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:
- Click Change Profiles on the panel.
- Select a component to which you want to map the profile. This can be either entire project, current module or current context.
- Select a profile to which you want to map this component.