Why you should use Spring Boot Dev Tools

Posted by Java developer blog on February 24, 2020

Overview

Spring Boot Dev Tools is a great tool that helps you to develop an application faster. It reflects changes in the code without the need to restart the whole application each time. In the post, we are going to discuss why and how to use Spring Boot Dev Tools.

How to enable

To enable Spring Boot Dev Tools simply add the following dependency in the pom file:

1
2
3
4
5
6
7
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

Flagging the dependency as optional in Maven or using the developmentOnly configuration in Gradle (as shown above) prevents devtools from being transitively applied to other modules that use your project.

Repackaged archives do not contain devtools by default. You may want to include it. Using the Maven plugin set the excludeDevtools property to false:

1
2
3
4
5
6
7
8
9
10
11
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludeDevtools>false</excludeDevtools>
            </configuration>
        </plugin>
    </plugins>
</build>

Run an application in debug mode. Then you could change code (change method, add method or class). After that, if you want to reflect changes in the code simply build the project again:

Spring Boot Dev Tools will automatically restart the application.

How does it work

Spring Boot Dev Tools uses two classloaders. The first or so-called base classloader loads classes that don’t change (third party libraries). The second or so-called restart classloader loads classes that change a lot (you classes). When Spring Boot Dev Tools restart the application it throws away and created a new restart classloader. It requires less time compared to the full application restart because it doesn’t restart the base classloader.

Conclusion

We have described why and how to use Spring Boot Dev Tools.