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, Spring Boot Devtools, Java at least 8, Eclipse

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'
	}
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

plugins {
    id 'java-library'
    id 'org.springframework.boot' version '2.2.2.RELEASE'
}

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:

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

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

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<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.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>at least 8</source>
					<target>at least 8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Now we 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 restart server

Next we will add a controller class and scanBasePackages on main class without restarting the server.

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

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 GreetController {

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

}

Add the following line on the main class and replace the @SpringBootApplication annotation.

(scanBasePackages = "com.roytuts.spring.boot.reload.changes.without.server.restart")

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

reload changes on spring boot without restart server

So we saw how to reload our code changes in Spring Boot application without restarting the server.

Thanks for reading.

Leave a Reply

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