How to reload Changes on Spring Boot without Server Restart

Introduction

Situations may occur when you need to speed up your development works without having to worry about restarting the server for every change in the piece of code in your application. Spring Boot provides such provision using which you can easily achieve the goal.

Applications that use spring-boot-devtools will automatically restart whenever files on the classpath change. This can be a useful feature when working in an IDE as it gives a very fast feedback loop for code changes. By default, any entry on the classpath that points to a folder will be monitored for changes.

With this dependency any changes you save, the embedded tomcat will restart. Spring Boot has a Developer tools (devtools) module helps to improve the productivity of developers.

Prerequisites

Spring Boot 2.2.2 – 2.4.5, Spring Boot Devtools, Java at least 8

Reload Code Changes without Restarting Server

Let’s create Spring Boot project in Eclipse IDE. The name of the project is spring-boot-reload-changes-without-server-restart.

The build.gradle script is given below with the required dependencies for gradle based project:

buildscript {
	ext {
		springBootVersion = '2.2.2.RELEASE' to 2.4.5
	}
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

plugins {
    id 'java-library'
    id 'org.springframework.boot' version "${springBootVersion}"
}

sourceCompatibility = 12
targetCompatibility = 12

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
    implementation("org.springframework.boot:spring-boot-devtools:${springBootVersion}")
}

The corresponding pom.xml file is given below for maven based project:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.roytuts</groupId>
	<artifactId>spring-boot-reload-changes-without-server-restart</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>12</maven.compiler.source>
		<maven.compiler.target>12</maven.compiler.target>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.5</version>
	</parent>

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

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

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

Now I will write main class to start up the Spring Boot application:

package com.roytuts.spring.boot.reload.changes.without.server.restart;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootChangesWithoutRestartServerApp {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootChangesWithoutRestartServerApp.class, args);
	}

}

Now running the above main class will deploy your application and server will be started on port 8080.

Now try to hit the root URL http://localhost:8080 or http://localhost:8080/greet, you will see below output on browser:

reload changes on spring boot without server restart

Next I am creating the following Spring REST controller class with the /greet endpoint.

package com.roytuts.spring.boot.reload.changes.without.server.restart;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

	@GetMapping("/greet")
	public String greet() {
		return "Greetings, Welcome!";
	}

}

Now on every save on your code changes the embedded Tomcat server will be reinitialized automatically.

Now hitting the URL http://localhost:8080/greet will give you the following output:

reload changes on spring boot without server restart

So you got an idea how to reload our code changes in Spring Boot application without restarting the server.

Source Code

Download

1 thought on “How to reload Changes on Spring Boot without Server Restart

Leave a Reply

Your email address will not be published. Required fields are marked *