Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are development tools?
3.
Spring Boot DevTools Features
3.1.
Automatic Restart
3.2.
Live Reload
3.3.
Remote Development Support
3.4.
Built-in Developer Tools
3.5.
Enabling the Spring Boot Dev Tools Module
3.5.1.
For Maven Based Project
3.5.2.
Inside the pom.xml file:
3.5.3.
For Gradle Based Project
3.5.4.
Inside build.gradle:
4.
Development tools of Spring Boot
5.
1..) Automatic Reload 
5.1.
1.1 Customizing Auto Reload
6.
2..) Caching the Static Resources
7.
3..) Automatic Restart
7.1.
3.1) Customising Auto Restart
7.1.1.
a..) Keeping Track of Auto-Configuration Delta Changes:
7.1.2.
b..) Turning off Restart: 
7.1.3.
c..) Restart by using a Trigger File
7.1.4.
d..) Limitations of Automatic Restart
8.
4..) Global Setting of Spring Boot dev tool
8.1.
4.1) Setting up File System Watcher
9.
5..) Remote Applications
10.
Frequently Asked Questions
10.1.
What are development tools?
10.2.
What do dev tools in spring boot mean?
10.3.
How do I make dev tools available in Spring Boot?
11.
Conclusion
Last Updated: May 19, 2024
Easy

Spring Boot DevTools

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

DevTools is short for development tools. A tool that facilitates the rapid deployment of applications on a programming platform is known as a development tool.

Spring Boot DevTools

The Spring boot dev tools module includes many useful developer features for improving the development experience, such as static resource caching, automatic restarts, live reload, global settings, and remote application running.
 

This article will discuss various development tools provided by spring boot. So, let's get started!

What are development tools?

A company named Pivotal maintains an open-source micro-framework known as Spring Boot. It provides a platform for Java developers to get started with an auto-configurable production-grade Spring application. Developers can use it to get started quickly without wasting time preparing and configuring their Spring application.

Spring Boot provides an additional set of tools to improve the application development experience. Any project can include the spring-boot-devtools module to provide additional development-time features.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Spring Boot DevTools Features

Automatic Restart

Spring Boot DevTools provides automatic restart functionality, allowing developers to quickly see changes made to their application without needing to manually restart the server. Any changes to class files trigger a restart of the application, speeding up the development process.

Live Reload

With live reload, developers can instantly see changes made to static resources such as HTML, CSS, or JavaScript files without needing to manually refresh the browser. This feature enhances the development experience by providing real-time feedback on frontend changes.

Remote Development Support

Spring Boot DevTools offers remote development support, enabling developers to connect their local development environment to a remote server. This feature allows for seamless development and debugging on remote servers, improving productivity in distributed development teams.

Built-in Developer Tools

Spring Boot DevTools includes various built-in developer tools such as endpoints for monitoring, logging, and configuration. These tools provide valuable insights into the application's runtime behavior and facilitate troubleshooting and debugging tasks during development.

Let’s see how to include this module in our project.

Enabling the Spring Boot Dev Tools Module

Simply add the following module dependency to your build to include devtools support:

For Maven Based Project

Inside the pom.xml file:

<dependencies>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-devtools</artifactId>

        <optional>true</optional>

    </dependency>

</dependencies>

For Gradle Based Project

Inside build.gradle:

dependencies {

compile("org.springframework.boot:spring-boot-devtools")

}

 

Now, let’s move on to see some of the important devTools of Spring Boot.

Development tools of Spring Boot

1..) Automatic Reload 

When a resource changes, the spring-boot-devtools module includes an embedded LiveReload server that can trigger a browser refresh. The browser must support an extension for it as a prerequisite.
 

Note: You can only have one LiveReload server running at any given time. Check that no other LiveReload servers are running before starting your application. Only the first application launched from your IDE will have LiveReload support.

 

Live reload is enabled by default. Set the spring.devtools.livereload.enabled property to false if you want to disable this feature for any reason.

 

Inside application.properties

spring.devtools.livereload.enabled  = false      
#Set false to disable live reload

1.1 Customizing Auto Reload

Sometimes we want only a few resources to get auto-reload. In that case, we can exclude resources we don't want to auto-reload, and we can similarly we can Include/Exclude Additional Paths as well.

 

Auto-reload works on the following paths by default:

  1. /META-INF/maven
  2. /META-INF/resources
  3. /resources
  4. /static
  5. /public
  6. /templates

If we want to disable browser auto-reload for files in a subset of these paths, we can use the spring.devtools.restart.exclude property.

spring.devtools.restart.exclude=static/**,public/**

 

There may be a few files that are not in the resources or the classpath, but we may still want to keep an eye on those additional files/paths to reload the application. Use the spring.devtools.restart.additional-paths property to accomplish this.

spring.devtools.restart.additional-paths=script/**

 

Similarly, if we want to keep the defaults while adding additional exclusions, use the spring.devtools.restart.additional-exclude property.

spring.devtools.restart.additional-exclude=styles/**

2..) Caching the Static Resources

To improve performance, dev tools cache static content/template files to be served faster to the browser/client. 

 

Caching is an excellent feature for production, where every millisecond of performance improvement is critical. However, it can cause a stale cache problem in a development environment, and we may not see our changes immediately in the browser. This capability can be customised using the Dev Tools module by configuring a few properties.

 

The caching feature is turned off by default. We can enable it for use in a production environment by setting a property. This feature is supported by a large number of UI template libraries. For example, thymeleaf, freemarker, groovy, moustache, and so on.

 

Inside application. properties:

#set true in production environment
#spring.freemarker.cache = true    

#set false in development environment; It is false by default.
spring.freemarker.cache = false      

#Other cache properties

spring.thymeleaf.cache = false
spring.mustache.cache = false
spring.groovy.template.cache = false

 

3..) Automatic Restart

You must be thinking, what is the difference between automatic reload and automatic restart? Let's clear this doubt first before moving any further. 

 

So, The term "auto-load" refers to the browser's UI reloading to see changes to static content. And reloading the server-side code and configurations, followed by a server restart, is referred to as “auto-restart”.

 

The term "auto-restart" refers to the server-side reloading of Java classes and configurations. Following server-side changes, these changes are dynamically re-deployed, and the server is restarted to load the modified code and configuration.

 

In layman’s terms, we can say that, When files on the classpath change, applications that use spring-boot-devtools will restart automatically. 

 

This can be a helpful feature when working in an IDE because it provides a very fast feedback loop for code changes. Any classpath entry that points to a folder is monitored for changes by default. It should be noted that some resources, such as static assets and view templates, do not require the application to be restarted.

3.1) Customising Auto Restart

a..) Keeping Track of Auto-Configuration Delta Changes:

By default, a report displaying the condition evaluation delta is logged each time your application restarts. The report displays changes to your application's auto-configuration as we make changes like adding or removing beans and changing configuration properties.

 

Set the following property to disable report logging:

spring.devtools.restart.log-condition-evaluation-delta = false

 

b..) Turning off Restart: 

If you do not want to use the restart feature, you can turn it off by setting the spring.devtools.restart.enabled property to false. In most cases, you can specify this in your application.properties as follows:

spring.devtools.restart.enabled = false

 

If you need to disable restart support completely, for example, because it doesn't work with a specific library, you must set a System property before calling SpringApplication.run(...)

 

As an example:

public static void main(String[] args) {
   System.setProperty("spring.devtools.restart.enabled", "false");
   SpringApplication.run(MyApp.class, args);
}

c..) Restart by using a Trigger File

Automatic restarts are not always desirable, and they can sometimes slow down development time due to frequent restarts. We can use a trigger file to solve this problem.

 

Spring boot will continue to monitor that file, and if it detects any changes, it will restart the server and reload all of your previous changes.
 

To specify the trigger file for your application, use the spring.devtools.restart.trigger-file property. It could be any file, external or internal.

spring.devtools.restart.trigger-file = c:/workspace/restart-trigger.txt

d..) Limitations of Automatic Restart

The restart functionality does not work well with objects deserialized with a standard ObjectInputStream. If you need to deserialize data, use Spring's ConfigurableObjectInputStream in conjunction with Thread.currentThread().getContextClassLoader().

 

Unfortunately, many third-party libraries deserialise without taking the context classloader into account. If you discover such a flaw, you must contact the original authors to request a fix.

4..) Global Setting of Spring Boot dev tool

Setting all of our favourite configuration options every time we create a new Spring boot project may become a redundant effort. We can reduce it by using a global setting file.

 

Individual projects/modules will inherit all custom settings from the global file, and they can override any specific setting on a per-project basis if necessary.

 

Go to your system's user's home directory and create a file called .spring-boot-devtools.properties. (Please keep in mind that file names begin with a dot.) 

 

This global property file should not be used to configure globally available options.

spring.devtools.restart.trigger-file = c:/workspace/restart-trigger.txt

 

On the other hand, we can add any of the following files to the $HOME/.config/spring-boot directory to configure global devtools settings:

  1. spring-boot-devtools.properties
  2. spring-boot-devtools.yaml
  3. spring-boot-devtools.yml

Any properties added to these files apply to all Spring Boot applications that use devtools on your machine. To make a restart, always use a trigger file. For example, add the following property to your spring-boot-devtools file:

spring.devtools.restart.trigger-file=.reloadtrigger

4.1) Setting up File System Watcher

FileSystemWatcher works by polling the class changes at a predefined time interval and then waiting for a predefined quiet period to ensure that no more changes occur. 

 

Because Spring Boot entirely relies on the IDE to compile and copy files into a location where Spring Boot can read them, you may notice that certain changes are not reflected when devtools restarts the application. 
 

If you are constantly experiencing such issues, try increasing the spring.devtools.restart.poll-interval and spring.devtools.restart.quiet-period parameters to the values that are appropriate for your development environment:

spring.devtools.restart.poll-interval=2s
spring.devtools.restart.quiet-period=1s

5..) Remote Applications

Spring Boot developer tools aren't just for local development. When running applications remotely, you can also use some features. 

 

Remote support is optional because enabling it poses a security risk. It should only be enabled when running on a trusted network or when SSL is used to secure the network. If you don't have either of these options, you shouldn't use DevTools' remote support. 
 

Support should never be enabled on a production deployment. To enable it, ensure that devtools is included in the repackaged archive, as shown in the list below:

<build>
   <plugins>
       <plugin>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-maven-plugin</artifactId>
           <configuration>
               <excludeDevtools>false</excludeDevtools>
           </configuration>
       </plugin>
   </plugins>
</build>

 

The spring.devtools.remote.secret property must then be set. Like any important password or secret, the value should be unique and strong enough to prevent guessing or brute-forcing.
 

There are two parts to remote devtools support: a server-side endpoint that accepts connections and a client application that runs in your IDE. When the spring.devtools.remote.secret property is set, the server component is automatically enabled. The client component must be manually launched.

 

This was all about devTools supported by Spring Boot. Now, let’s move to some of the frequently asked questions on this topic.

Frequently Asked Questions

What are development tools?

A development tool is a computer programme used by the developers to create, debug, maintain, or support other programmes and applications.

What do dev tools in spring boot mean?

DevTools is an abbreviation for Developer Tool. Its main goal is to try to reduce development time while working with the Spring Boot application. Spring Boot DevTools pick up the changes, and the application is restarted. 

How do I make dev tools available in Spring Boot?

It is very simple to enable dev tools in a spring boot application. Simply include the spring-boot-devtools dependency in your build file.

Conclusion

In this blog, we have learned about the various Spring Boot DevTools. Spring Boot DevTools significantly enhances the development experience for Spring Boot applications by providing a suite of powerful features. From automatic restarts to live reloads and remote development support, DevTools streamline the development process, allowing developers to iterate quickly and efficiently. 

Recommended Readings:

 

You can also consider our Spring Boot Course to give your career an edge over others.

Keep Learning with Coding Ninjas!

Live masterclass