Use DevTools in Spring Boot App

When we are running Spring Applications. If we are making modifications to our source code of the applications. Then we have to manually restart the server to update those modifications in our Spring Boot Application. This is the problem when we are working on a large application and then we are making some minor changes to it. Thus, we will restart the server and it will take to time run the application.
DevTools is providing a solution to this problem. When we are using DevTools then our Spring Boot App is automatically restarted.
Spring Boot 1.3 is provided with a new module called spring-boot-devtools. This module’s goal is to try and build up development-time experience while working on Spring Boot applications.
To use this module we simply need to add a dependency on your Maven(pom.xml) file in the application:

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

This module spring-boot-dev tool is having nice features are:
   • Property Defaults
   • Automatic Restart
   • LiveReload
   • Remote Debug Tunneling
   • Remote Update and Restart 
Property Defaults
If we are working with template technology Thymeleaf. The template technology is having properties called spring.thymeleaf.cache.The properties can disable cache and allow updating pages without the need to restart the application. Setting up the properties during the development time of the application creates some issues.
But using this spring-boot-dev tools module we don’t need to set up these properties during development caching Thymeleaf, Freemaker, Groovy template, Velocity and Mustache are all automatically disabled.

Automatic Restart
Reloading the java class and configuring it on the server side after some modification is done in the source code in our application, automatically redeploy the application onto the server.

LiveReload
This spring-boot-dev tools module provides us with the LiveReload feature. The LiveReload feature is related to the view page whenever we make some modifications to the resources of the application then it will refresh the browser automatically with changes that reflect the view page.

Remote Debug Tunneling
Java remote debugging is very useful for us when we are diagnosing issues on remote applications. But it is not possible all the time because when applications are deployed outside of your data centre. Remote debugging is tricky when we are setting-up container-based technology like Docker.
To get the better of these limitations spring-boot-dev tools provide us with tunnelling of remote debugging support over HTTP protocol. The remote client provides us with local server port 8000 in so you can attach a remote debugger to it. When the connection is established then remote debug traffic is sent over the HTTP protocol to the remote application.


You can change the port with the use  of this property below here:
spring.devtools.remote.dedug.local-port

Remote Update and Restart 
This spring-boot-devtools module is providing a feature to update and restart the remote applications. The remote client is monitoring our application classpath for modifications in the same manner in the local restart. Any modification is done in our resource then it will be pushed onto the remote application and if needed restart. Thus, it is triggered to restart the remote application. This feature is very helpful when are using cloud services that we don’t have locally. In general remote update and restart of the application is much quicker than the full rebuild and deploy cycle.

In this we will make some changes in the source code then our application automatically restart.

Development Process:
1. Keep eclipse IDE ready(STS integrated)
2. Create a Spring Boot Starter Project 
3. Create a RestController class
4. Run the Project

1. Keep eclipse IDE ready(STS Integrated) 
Refer to this article How to Create Spring Project in IDE to create Spring Boot Project in Eclipse IDE.

2. Create a Spring Boot Starter Project 
 Add the following dependencies: 
    • Spring Web
    • Spring Boot Devtools

spring_boot_devtools

Maven Dependency
pom.xml:

   <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.6.2</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>SpringBootWithDevTools</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>SpringBootWithDevTools</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>11</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

3. Create a RestController class
HomeController.java:

package com.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {
	@GetMapping("/")
	public String msg() {
		return "Hello DevTools(Auto Restart Spring Boot App)";
	}
}

4. Run the Project
Right-Click on the SpringBootWithDevToolsApplication.java class then Run it as Java Application. Just make some code changes on the code and then see the magic of the Devtools automatically restart the application.

Output:
Go to the Browser and type this URL http://localhost:8080

spring_boot_devtools

Conclusion:
This example is explained What is the use of the DevTools in the example? What are the features of Devtools? How can we resolve the manual restart server problem of the Spring Boot Application?

Leave a Comment