Open In App

Hot Reload with Spring Boot DevTools

Last Updated : 20 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Hot reloading allows developers to see changes made to their application in real-time without restarting the server. In Spring Boot, this is achieved through Spring Boot DevTools. This tool significantly enhances development by reducing the time required to see changes in the application.

Hot Reload with Spring Boot DevTools

Spring Boot DevTools is a module that provides several development-time features, including hot reloading. It automatically restarts the Spring Boot application whenever it detects changes in the code, enabling faster development cycles and improving productivity.

Key Features:

  • Automatic Restart: Automatically restarts the application when changes are detected.
  • LiveReload Integration: Automatically refreshes the browser after a change is made.
  • Property Defaults: Provides sensible defaults for properties to ease development.

Prerequisites:

  • Basic understanding of Spring Boot.
  • Maven for dependency management and building the project.
  • JDK and IntelliJ IDEA (or any preferred IDE) installed on your system.

Implementation of Hot Reload with Spring Boot DevTools in Spring Boot

Step 1: Create a New Spring Boot Project

Create a new Spring Boot project using IntelliJ IDEA or any preferred IDE. Choose the following options:

  • Name: spring-DevTools-Demo
  • Language: Java
  • Type: Maven
  • Packaging: Jar

Click on the Next button.

Project Metadata

Step 2: Add the Dependencies

Add the following dependencies into the Spring Boot Project.

  • spring-boot-starter-web: Adds web-related capabilities to the project, enabling the development of RESTful services.
  • spring-boot-devtools: Provides development-time features like hot reloading and live reload integration.
  • lombok: Reduces boilerplate code by generating getters, setters, and other methods at compile time.
Dependencies

Project Structure:

Once the project is created, the file structure should look something like this:

Project Folder Structure

Step 3: Main Class

The main class SpringDevToolsDemoApplication is automatically generated and serves as the entry point for the Spring Boot application. No changes are required here unless additional configurations are needed.

Java
package com.gfg.springdevtoolsdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class SpringDevToolsDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringDevToolsDemoApplication.class, args);
    }
}

pom.xml file:

XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gfg</groupId>
    <artifactId>spring-DevTools-Demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-DevTools-Demo</name>
    <description>spring-DevTools-Demo</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Step 4: Run the Application

After setting up the project, run the application. By default, it will start on port 8080. You can verify that the application is running by accessing http://localhost:8080.

Application Runs

Step 5: Enable the Automatic Restart

By default, Spring Boot DevTools will restart the application whenever files in the src/main/java or src/main/resources directories are changed. No additional configuration is required to enable this feature.

Step 6: Add the Endpoint

Add the following endpoint to the main class. This endpoint will display a simple message.

    @GetMapping("/")
    public String home() {
        return "Hello, Spring Boot DevTools!";
    }

After saving the file, the application will automatically restart, and the changes will be reflected immediately without needing to restart the server manually.

  • @GetMapping("/"): Maps HTTP GET requests to the / path, returning a simple string message.
  • home() Method: A simple method that returns "Hello, Spring Boot DevTools!" when the root URL is accessed.
Application Restarts

Conclusion

Spring Boot DevTools significantly enhances the development process by enabling hot reloading, which reduces downtime and improves productivity. By simply adding the DevTools dependency, you can see code changes in real-time, making development faster and more efficient.


Next Article

Similar Reads