Open In App

Spring Boot – DevTools

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:

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-




<!--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.

Console

Global setting config for Spring Boot DevTools

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

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:

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:

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.

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.


Article Tags :