English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SpringBoot DevTools

Spring Boot DevTools

Spring Boot 1.3It provides another module named Spring Boot DevTools. DevTools stands for Developer Tool . The purpose of this module is to try and shorten development time when using Spring Boot applications. Spring Boot DevTools accepts changes and restarts the application.

We can implement DevTools in our project by adding the following dependencies to the pom.xml file.

<dependency>
<groupId>org.springframework.boot</groupId>/groupId>
<groupId>-boot-<artifactId>spring</artifactId>/<artifactId>devtools</artifactId>
<scope>runtime</scope>
</dependency>

Spring Boot DevTools features

Spring Boot DevTools provides the following features:

Default property values Automatic restart LiveReload Remote debugging tunnel Remote update and restart

Default property values: : Spring Boot provides template technology Thymeleaf , which includes the property spring.thymeleaf.cache.. Cache and allow us to update the page without restarting the application. But setting these properties always brings some problems during the development process.

When we use spring-boot-when the devtools module is used, there is no need to set the property. It will be automatically disabled during the development cache of Thymeleaf, Freemarker, and Groovy templates.

Note: If we do not want to apply the default property values to the application, we can set configprop: spring.devtools.add in the application.properties file-properties [] set to false.

Automatic restart: Automatic restart means reloading Java classes and configuring them on the server side. After changes on the server side, it is dynamically deployed, the server restarts, and the modified code is loaded. It is mainly used for microservices-based applications. Spring Boot uses Twothe ClassLoader of type:

The unchanging classes (two-thirds) are loaded intothe basic ClassLoader. The classes we are actively developing are loaded intoRestarting in the ClassLoader.

When the application restarts, the restartable ClassLoader is discarded, and a new class is filled in. Therefore, the basic ClassLoader is always available and filled.

We can use the property spring.devtools.restart.enabled Set to to disable the server's automatic restart. Error.

Remember:

DevTools always monitors the classpath resources. The only way to trigger a restart is to update the classpath. DevTools requires a separate application class loader to work properly. By default, Maven derives the application process. Automatic restarts with LiveReload works well together. DevTools relies on the application context's shutdown hook to close it during restarts.

LiveReload: The Spring Boot DevTools module includes a module named LiveReload's embedded server.As long as we make changes to it, the application can automatically trigger browser refresh. Resources. This is also known as auto-refresh.

Note: We can disable LiveReload by setting the property disable LiveReload, enableTo false.

It provides browser extensions for Chrome, Firefox, and Safari. By default, LiveReload is enabled. LiveReload works on the following paths:

/META-INF/maven /META-INF/resources /resources /static /public /templates

We can also disable automatic reloading in the browser by excluding the above paths. For example:

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

We can use the property spring.devtools.restart.additional-paths to view other additional paths.For example:

spring.devtools.restart.additional-paths=/path-to-folder

If we want to exclude other paths while keeping the default path, we can use the property spring.devtools.restart.additional-exclude.For example:

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

Remember

We can run one LiveReload server at a time. Before starting the application, make sure that no other LiveReload servers are running. If we start multiple applications from the IDE, it only supports the first LiveReload.

Remote debugging tunnel: Spring Boot can tunnel JDWP (Java Debug Wire Protocol) directly to the application via HTTP. It can even deploy the application to ports that are only publicly exposed.80 and443Internet Cloud providers.

Remote update and restart: Another trick provided by DevTools is: it supports remote applications updateand restart.it monitors file changes in the local classpath and pushes them to a remote server, then restarts. We can also combine this feature with LiveReload.

trigger file

Due to frequent restarts, automatic restarts can sometimes slow down development time. To solve this problem, we can use a trigger file.. SpringBoot monitors the trigger file and detects changes in the file. It will restart the server and reload all previous changes.

We can add properties spring.devtools.restart.trigger-file is implemented to trigger files in our application.Is it internal or external. For example:

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

Spring Boot DevTools Example

Steps1: Use Spring Initializr https://start.spring.io/.

Steps2: provided by groupIdname and artifact groupId. We provide codebox3artifactId and spring-boot-devtools-example.

Steps3: Add the following dependencies: spring-boot-starter-web and spring-boot-devtools .

Steps4: Click GenerateButton. It will download the project's Jar File.

Steps5: Extract Jar file.

Steps6: Import the folder into STS.

File-Import-Existing Maven Project-Browse-Select folder spring-boot-devtools-example-Complete

After successful import, we can see the following directory in the Package Explorer section of STS.

Steps7: Open SpringBootDevtoolsExampleApplication.java And run it as a Java application.

Then, make any changes (edit or delete some files or code) in the application, and then save the changes. After saving the changes, the server will restart and fetch the changes.