Spring Boot Devtools Tutorial

If you have worked on latest UI development frameworks e.g. Node, angular, gulp etc. then you must have appreciated the auto-reload of UI in browser whenever there is change in some code. Its pretty useful and saves a lot of time.

Well, same feature can be utilized in spring boot applications using spring-boot-devtools dependency provided features. Let’s learn about enabling these features and using them.

Table of Contents

Enabling Dev Tools Module
Static Resource Caching
Automatic UI refresh
	- Excluding Resources
	- Watching Additional Paths
Automatic server restart
	- Enable/disable logging of auto-configuration changes
	- Disabling Restart
	- Using a Trigger File
Global settings file

Enabling Dev Tools Module

To enable dev tools in spring boot application is very easy. Just add the spring-boot-devtools dependency in your 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")
}

Static Resource Caching

To improve the performance, dev tools cache the static content/template files to serve them faster to browser/client. This is very good feature in production where every milli-second performance improvement matters. But in development environment, it can be a problem and cause stale cache problem and you may not see your changes immediatly in browser. Dev tools module provide this capability by setting few properties.

By default, this feature is disabled. You 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 such properties

spring.thymeleaf.cache = false
spring.mustache.cache = false
spring.groovy.template.cache = false
Auto-refresh vs Auto-restart – Auto-refresh (or auto-load) refer to UI reload at browser to see static content changes. Auto-restart is referred to reloading the server side code and configurations followed by server restart.

Automatic UI refresh

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 your browser should have supported extention for it. You can find such browser extentions in this link.

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

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 you 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/**

Watching/Excluding Additional Paths

There may be few files not in classpath, but you 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 those defaults and add additional exclusions, use the spring.devtools.restart.additional-exclude property instead.

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

Automatic server restart

Auto-restart means reloading the java classes and consiguration at server side. After the server side changes are re-deployed dynamically, server restart happen and load the modified code and configutation.

Enable/disable logging of auto-configuration 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 you 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

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

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, you 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

Global settings file

Setting all your favorite configutation options everytime for all your spring boot projects or modules may become a duplicate effort. You can minimize it using a global setting file. Then 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

Drop me your questions in comments section.

Happy Learning !!

Reference: Spring boot Ref

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

5 thoughts on “Spring Boot Devtools Tutorial”

  1. Is the devtools library can able to detect XML files also,when we do any modifications in XML file.with out restart the server.

    Reply
  2. Can you please describe the restart-trigger.txt in detailed….? means what are the parameters used to and what are the allowed params and values…? Thanks

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.