Spring Boot Dev Tools Tutorial

The Spring boot dev tools module provides a lot of useful developer features for improving the development experience such as caching static resources, automatic restarts, live reload, global settings and running the remote applications.

1. Enabling Dev Tools Module

To enable dev tools in the spring boot application is very easy. Just add the spring-boot-devtools dependency in the build file.

Maven

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

Gradle

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

2. Chaching the Static Resources

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

Caching is a very good feature in production where every millisecond of performance improvement matters. But in development environment, it can be a problem and cause stale cache problem and we may not see our changes immediatly in browser. Dev tools module can customize this capability by setting few properties.

By default, the caching feature is disabled. We can enable it to use in production environment by setting a property.

There are many such UI template libraries that support this feature. e.g. thymeleaf, freemarker, groovy, mustache etc.

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

#Other cache properties

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

3. Automatic Reload

The spring-boot-devtools module includes an embedded LiveReload server that can be used to trigger a browser refresh when a resource is changed. Precondition is that the browser should have supported extention for it. 

By default, live reload is enabled. If you wish to disable this feature for some reason, then set spring.devtools.livereload.enabled property to false.

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

3.1. Excluding Resources from Auto Reload

By default, Auto-reload works on these paths:

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

If we want to disable auto-reload in browser for files in few of these paths, then use spring.devtools.restart.exclude property. e.g.

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

3.2. Include/Exclude Additional Paths

There may be a few files not in the resources or the classpath, but we still may want to watch those addtional files/paths to reload the application. To do so, use the spring.devtools.restart.additional-paths property.

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

Similarily, If you want to keep the defaults and add additional exclusions, use the spring.devtools.restart.additional-exclude property instead.

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

4. Automatic Server Restart

Auto-restart means reloading the Java classes and configurations on the server-side. After the server side changes, these changes are re-deployed dynamically, Server restart happens and load the modified code and configutation.

4.1. Logging Auto-Configuration Delta Changes

By default, each time your application restarts, a report showing the condition evaluation delta is logged. The report shows the changes to your application’s auto-configuration as we make changes such as adding or removing beans and setting configuration properties.

To disable the logging of the report, set the following property:

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

4.2. Disabling Restart

To disable the restart of server on non-static code changes, use the property spring.devtools.restart.enabled.

spring.devtools.restart.enabled = false

4.3. Restart Using a Trigger File

Automatic restarts may not be desirable on every file change and sometimes can slower down development time due to frequent restarts. To solve this problem, we can use a trigger file.

Spring boot will keep monitoring that file and once it will detect any modification in that file, it will restart the server and reload all your previous changes.

Use spring.devtools.restart.trigger-file property to mention the trigger file for your application. It can be any external or internal file.

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

Auto-Reload vs Auto-Restart

Auto-refresh (or auto-load) refers to UI reload at the browser to see static content changes.

Auto-restart is referred to reloading the server-side code and configurations followed by a server restart.

5. Global Settings File

Everytime we create a new Spring boot project, setting all the favorite configutation options may become a duplicate effort. We can minimize it using a global setting file.

The individual projects/module will inherit all custom settings from global file, and if needed they can override any specific setting per project basis.

To create global file, go to your system’s user’s home directory and create a file named .spring-boot-devtools.properties. (Please note that file name start with a dot). Not use this global property file to configure globally available options.

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

Happy Learning !!

Reference: Spring boot Ref

Leave a Reply

5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.