Introduction about DevTools in Spring Boot

DevTools is a set of Spring Boot tools that make it easy to develop our Spring Boot applications. Imagine: every time we change the code of an application, we usually have to restart the application to see the change, using Spring Boot DevTools we do not need to do this; or if you are working with a web application with UI, each time you change the code to change the interface, you have to refresh the browser to see this change, using DevTools you can set up the browser can automatically reload to see the change. Attractive, right? How is it in details? Let’s find out in this tutorial.

First, I will create a new Spring Boot project with Thymeleaf Starter and DevTools dependencies as follows:

Introduction about DevTools in Spring Boot

Result:

Introduction about DevTools in Spring Boot

I will add a new controller:

with the home.html template file has the following content:

You can read the tutorial on Using Thymeleaf in Spring Boot to understand more about this project.

At this point, if you run this example application, you will see the results as follows:

Introduction about DevTools in Spring Boot

Now, if you try to change the code of HomeController to print “Huong Dan Java”, as follows:

Immediately, you will see in the console window, our application will automatically restart and the text we just added will be displayed if you refresh the browser.

Introduction about DevTools in Spring Boot

In case if your browser has the LiveReload plugin installed (https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei for Chrome browser), then you do not need to refresh manually. When we change any code in our application, the browser will automatically reload the page.

For example, now we will change the code in the HomeController in the above example to:

Immediately, I will see the browser automatically refresh and display the results immediately:

Introduction about DevTools in Spring Boot

This is a great feature, right? Now we will save a lot of time for the development of a Spring Boot application, right? Note that when using LiveReload you have to enable it first.

DevTools allows us to exclude resources in our project that you do not want the application to restart whenever we change the code for those resources.

By default, the changes to the resources in the directories:

  • /META-INF/maven,
  • /META-INF/resources,
  • /resources,
  • /static,
  • /public,
  • /templates

DevTools will not restart the application, only reload with LiveReload.

You can add the spring.devtools.restart.exclude property to the application.properties file to override these default values, the value of which is the list of resources to be excluded.

For example, I just need to exclude all the resources in the /static directory, I will declare the spring.devtools.restart.exclude property as follows:

If you want to keep the default resource excluded from restarting and adding new resources then you can use the property “spring.devtools.restart.additional-exclude”. The value of this property is the other resources the default values that you want to exclude.

In addition to these functions, DevTools also allows us to disable the caching feature of some libraries.

Some libraries supported by Spring Boot use caching to improve performance, such as Thymeleaf, etc. This will be beneficial to production as we don’t have any changes in the code. However, in terms of development, we do not need this function because our code changes frequently. Therefore, DevTools by default will disable the caching feature of these libraries.

1/5 - (1 vote)

One thought on “Introduction about DevTools in Spring Boot

Add Comment