Open In App

Spring Boot – DevTools

Last Updated : 08 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Spring Boot provides DevTools, a module designed to ease your development process, it improves the experience of developing applications resulting in optimising the developer’s productivity for building exceptional software.

It aims to reduce development time, by intelligently detecting code changes to auto-recompile your code reducing the need to manually restart your application.

Spring Boot DevTools offers many other development-time features listed below, one among them is explained above as auto-recompilation of code:

  • Property DefaultstoAutomatic Restart
  • Live Reload
  • Global Settings
  • Remote Debug Tunneling
Working of Spring Boot - DevTools

Working of Spring Boot – DevTools

Note: It is important to understand that the ‘DevTools’ is not an IDE plugin, nor does it require that you should use a specific IDE.

1. DevTools Configuration in Spring Boot

There are a few configurations required to use DevTools in your Spring Boot Application

1.1 Spring Boot Devtools Dependency

For Maven build automation tools dependency is-

XML




<!--Maven pom.xml-->
 
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <scope>runtime</scope>
</dependency>


For Gradle build automation tools dependency is-

dependencies {     compile("org.springframework.boot:spring-boot-devtools")   }  
OR
dependencies { developmentOnly("org.springframework.boot:spring-boot-devtools") }

1.2 Application Properties Settings for Spring Boot DevTools

Some configurations are available for application properties files to use Spring Boot DetTools

1.2.1 Toggle DevTools

It can be used to customize the detection of classpath changes to trigger auto restart if your Spring Boot Application

spring.devtools.restart.enabled=true

1.2.2 Exclude Resources from DevTools

It can be used to specify certain resources that will excluded to trigger a restart of your Spring Boot Application.

spring.devtools.restart.exclude=resources/**,web-inf/**

for the above code ‘resources’ and ‘web-inf’ resources are excluded

1.2.3 Enable/Disable LiveReloader for DevTools

To customize the use of LiveReloader use this configuration

 spring.devtools.livereload.enabled=true/false

1.3 Verify Spring Boot DevTools Configuration

On adding DevTools dependencies, the word ‘devtools’ appears alongside your project name.

The below images depict that the project has dev tools in a folder structure and Spring Boot Dashboard.

Folder Structure demonstrating DevTools is enabled in your project

Boot Dashboard in STS

Note:

  1. DevTools is neither a plugin nor an IDE-specific functionality, it works equally well in the STS ( Spring Tool Suite ), IntelliJ IDEA, and also Netbeans. 
  2. DevTools were built only for development-specific purposes, it is very smart enough that it disables itself when the application is deploying in a production setting.

2. Usage of Spring Boot DevTools

DevTools provides the Spring Boot developers with some development time tools that ease the development process. Some of these important workings are as follows-

2.1 Automatic application restart

With DevTools when we make changes to Java code or properties file, the application gets updated with new changes. It monitors for changes and automatically restarts the application. A few more points regarding automatic restart are listed.

  • With the DevTools in the application’s classpath, the application is loaded in two different class loaders in the Java Virtual Machine.
  • One class loader contains all the dependencies ( Libraries ) that applications need. These aren’t likely to change often.
  • The second class loader contains your actual Java code, property files, etc particularly in ( src/main/ path ) which are likely to change frequently.
  • On detecting a change, DevTools reloads only above the class loader by restarting the Spring application context and leaving another class loader, JVM intact.
  • This will help reduce the amount of time to start the application.
Console

Console

Global setting config for Spring Boot DevTools

Devtools provides a way to configure global settings that are not coupled with any application.

  • This file is named .spring-boot-devtools.properties and is located at $HOME.
  • If it is not configured manually, then defaults will be used by DevTools automatically.
Console

Console

Note: On automatic application restart, the changes to dependencies won’t be available. Because the respective class loader isn’t automatically reloaded. Therefore, after making the changes to dependencies, you have manually restart the application to get those changes in effect.

2.2 Automatic disable of template caches project

Spring Boot offers various template options like Thymeleaf, Groovy, FreeMarker, etc. These templates are configured to automatically cache the results of the template parsing. So, there is no need for them to be reparsed with every request that they serve.

Cached templates make it difficult to make the changes to the templates while the application is running and see the desired changes after refreshing the browser. Even after applying the changes, the cached template will still be in use until you restart the application.

Optimisation by DevTools for templates is listed:

  • This makes the Cached templates poor from the development perspective.
  • DevTools helps us with this by automatically disabling all template caching.
  • Due to this, your applied changes will now reflect on your templates after refreshing the browser.

Note: 

  1. You can additionally turn on/off caching of templates by specifying ‘spring.thymeleaf.cache=true/false‘ in the ‘application.properties’ file.
  2. You can also override other properties as well in the same above file.

2.3 Spring Boot Devtools as Live Reloader Server

If we have made some changes to the code and try to save it, then we have to click the refresh button on the browser each time to see its effect. This process automatically becomes time-consuming. It is always useful to witness the results in the browser and skip the refreshing of the browser again and again to reduce the development time.

The solution provided by Spring Boot DevTools is listed:

  • DevTools relieves us by skipping this cumbersome work for you.
  • When we embed and use DevTools, it automatically enables a ‘LiveReload’ server along with your application.
  • LiveReload server isn’t of much use, but it is useful when coupled with a corresponding LiveReload browser plugin.
  • This makes your browser automatically refresh when the changes are made to the files that are served directly or indirectly by your browser. Examples – Template engines, CSS stylesheets, JavaScript, and so on.
  • LiveReload has browser plugins for browsers like Google Chrome, Firefox, and Safari.
  • Internet Explorer and Edge browsers don’t have LiveReload browser plugins.
Console

Console

2.4 H2 Console

Your application additionally may require a database. Spring Boot offers you a very essential feature of an embedded H2 database which is very helpful for development purposes.

  • When we add an H2 database dependency in the application build, DevTools will automatically enable an H2 console.
  • You can access this console from your web browser itself.
  • This helps us to get an insight or overview of the data that is used or handled by the application.

To access this console, you have to simply point your web browser to – http://localhost:8080/h2-console.

Conclusion

In this article, we have discussed the below-mentioned usage of Spring Boot Devtools.

  • Automatic application restart
  • Automatic disable of template cache
  • Live Reloader Server
  • Auto-Enable H2 Console


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads