IntelliJ IDEA 2023.2 Help

Spring Boot

Spring Boot is an extension of the Spring framework that simplifies the initial configuration of Spring applications. It enables you to quickly create a working standalone Spring application with minimum default configuration.

Spring Initializr is a web application that can generate a Spring Boot project. You can select the necessary configuration, including the build tool, language, version of the Spring Boot framework, and any dependencies for your project. IntelliJ IDEA provides the Spring Initializr project wizard that integrates with the Spring Initializr API to generate and import your project directly from the IDE.

Create a new Spring Boot project via the Spring Initializr wizard

  1. Go to File | New | Project.

  2. In the left pane of the New Project wizard, select Spring Initializr.

  3. Go through the steps of the Spring Initializr project wizard.

For an example, refer to Tutorial: Create your first Spring application.

Spring Initializr generates a valid project structure with the following files:

  • A build configuration file, for example, build.gradle for Gradle or pom.xml for Maven.

  • A class with the main() method to bootstrap the application.

  • An empty JUnit test class.

  • An empty Spring application configuration file: application.properties

By default, IntelliJ IDEA applies code formatting to the generated files. If you want the files to remain formatted as they are generated by Spring Initializr, open the IDE settings with Control+Alt+S, select Languages & Frameworks | Spring | Spring Initializr and disable the Reformat code when creating a new project option.

Run a Spring Boot application

  • Open the class with the main() method (it is usually also designated with the @SpringBootApplication annotation), click The Run icon in the gutter, and select to run the class.

    Run Spring Boot application from the gutter

    Alternatively, you can press Control+Shift+F10 with the class file open in the editor.

IntelliJ IDEA creates and executes the Spring Boot run configuration. For more information, refer to Spring Boot run configuration.

Use Gradle to run Spring Boot

By default, for Spring Boot Gradle-based applications, IntelliJ IDEA uses Gradle to build the project and IntelliJ IDEA to run it. You can configure the IDE to use Gradle instead of IntelliJ IDEA to run Spring Boot applications.

  1. Go to the Advanced Settings page of the IDE settings Control+Alt+S.

  2. Under Frameworks. Spring Boot select Run using Gradle.

The Run using Gradle checkbox has no effect if IntelliJ IDEA is selected in Build, Execution, Deployment | Build Tools | Gradle | Build and run using. In this case, IntelliJ IDEA will be used to both build and run your Spring Boot application.

Custom configuration files

Spring Initializr creates one default configuration file that may not always be sufficient for development. 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 defined in your project.

Let IntelliJ IDEA know which files are configuration files in your project to enable relevant highlighting and coding assistance:

  1. Go to File | Project Structure or press Control+Alt+Shift+S to open the Project Structure dialog.

  2. Add the Spring facet: from the left-hand list, select Facets, click the Add icon, and select Spring.

  3. In the right-hand section, select Configuration Files and click Customize Spring Boot (Customize Spring Boot) in the toolbar.

  4. If you want to use a custom configuration file instead of the default one, type its name in the spring.config.name field.

    If you want to use multiple configuration files, click The Add button and select files from the project tree.

    Valid configuration files are marked with The Spring Boot icon.

  5. Click OK and apply the changes.

Configuring a custom configuration file

Runtime endpoints

Spring Boot includes additional features for monitoring and managing the state of your application in the production environment through HTTP endpoints or with Java Management Extensions (JMX). For more information, refer to Spring Boot Actuator: Production-ready Features.

Enable the Spring Boot actuator endpoints

  • Add the Spring Boot Actuator dependency for your project.

    Open the pom.xml file and add the following dependency under dependencies:

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>

    Open the build.gradle file and add the following dependency under dependencies:

    implementation 'org.springframework.boot:spring-boot-starter-actuator'

When you run your application with this dependency, you will be able to access the exposed actuator endpoints via HTTP. For example, if the application is running on localhost port number 8080, the default URL for the health endpoint will be http://localhost:8080/actuator/health.

View the Spring Boot actuator endpoints

  1. Run your Spring Boot application and open the Services tool window: select View | Tool Windows | Services or press Alt+8.

  2. Select your running Spring Boot application and open the Actuator tab.

Spring actuator beans shown in the Services tool window

You can use tabs to view endpoints of the following types: runtime beans, health information, and request mappings.

Beans

The Beans tab under Actuator shows the runtime beans of your Spring Boot application. Double-click any bean to open its declaration in the editor. These beans are indicated using the Spring Live bean icon in the gutter. Click this icon to view dependent and injected beans.

The Beans tab includes the following toolbar actions:

Action

Description

The Refresh button Refresh

Refresh the runtime beans information collected by the JMX agent.

The Diagram Mode button Diagram Mode

Show the complete graph for all your runtime beans instead of a list.

Required plugin: Diagrams (bundled).

The Show Library Beans button Show Library Beans

Show beans from libraries.

The Show Contexts button Show Contexts

Show available Spring application contexts.

The Show Configuration Files button Show Configuration Files

Show available configuration files.

The Show Bean Documentation button Show Bean Documentation

Show the documentation for the selected bean.

The Show Bean Graph button Show Bean Graph

Show the direct dependencies for the selected bean.

Required plugin: Diagrams (bundled).

Health

The Health tab under Actuator shows the status of your application. There are some auto-configured health indicators and you can also write custom health indicators.

For more information, refer to Health.

Mappings

The Mappings tab under Actuator shows the request mappings of your application. It lists all methods with the @RequestMapping annotation or its shortcuts, such as @GetMapping.

If you click the path mapping URI, you can select to run the corresponding HTTP request, open an HTTP requests file with the request, or open the request URL in the web browser (if it's a GET request). For more information, refer to HTTP Client.

Opening HTTP request mappings from Services tool window

Double-click a method to open its declaration in the editor. Spring registers such methods as handlers and IntelliJ IDEA indicates them with the Spring request mapping icon in the gutter. Click this icon to run the corresponding HTTP request, open it in a requests files, or in the web browser (if it's a GET request).

The Mappings tab includes the following toolbar actions:

Action

Description

The Refresh button Refresh

Refresh the request mappings collected by the JMX agent.

The Open in Browser button Open in Browser

Open the root application URL in a web browser.

The Request Method menu Request Method

Select which request methods to show.

The Show Library Mappings button Show Library Mappings

Show request mappings from libraries.

Application update policies

With the spring-boot-devtools module, your application will restart every time files on the classpath change. If IntelliJ IDEA is configured to continuously compile changed files, you can set a trigger file. In this case, your application will restart only after you modify the trigger file. For more information, refer to Automatic Restart.

Enable automatic restart

  • Add the spring-boot-devtools module dependency for your project.

    Open the pom.xml file and add the following dependency under dependencies:

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>

    Setting the spring-boot-devtools dependency as optional prevents it from being used in other modules that use your project.

    Open the build.gradle file and add the following dependency under dependencies:

    developmentOnly("org.springframework.boot:spring-boot-devtools")

    Setting the spring-boot-devtools dependency as developmentOnly prevents it from being used in other modules that use your project.

To update a running application, select Run | Debugging Actions | Update Running Application Command F10 from the main menu, or select your application in the Services tool window and click Update application. Depending on your needs, you can configure what the IDE will do when you execute this action.

Configure the application update policy

  1. Go to Run | Edit Configurations.

  2. Select the necessary Spring Boot run configuration to open its settings. Click Modify options.

    Configuring Spring Boot application update policy
  3. In the list that opens, point to On 'Update' action. You can choose to update only the resources, update both the classes and the resources (build your application), update the trigger file (which will trigger a restart), or try to perform a class hot swap, and if it fails, update the trigger file.

  4. In the Modify options list, point to On frame deactivation and select an action that the IDE will do after you switch to another application: update the resources, or build your application.

Last modified: 11 October 2023