Spring Boot Hot Swap or Automatic Restart

Spring Boot has a nice feature that enables the application to automatically restart when you change your Java code. This automatic restart of your application is often called Hot Swap.

Spring Boot Dev Tools Dependency

To enable your Spring Boot application to automatically restart when you make a change to your Java code, add the following dependency to your pom.xml file.


Here is an example of how a very simple pom.xml file looks like when a spring-boot-devtools dependency is added to the list of dependencies.

<?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">
        <relativePath/> <!-- lookup parent from repository -->
    <description>Demo project for Spring Boot</description>






How to Start Your App for Automatic Restart to Work

For your application to automatically restart when a change to Java code is made, you need to either start your Spring Boot App using a development environment like, for example, Netbeans or IntelliJ, or you need to start your application using maven command like it is in the example below.

mvn spring-boot:run

Then, depending on the development environment you use, after a change to Java code is made, you might need to build your project.

Note: If you start your Spring Boot app with java -jar, the How Swap will not work even if you add the spring-boot-devtools dependency.

Disable Hot Swap If Needed

If you need to temporarily disable the Hot Swap feature, you can set the spring.devtools.restart.enabled property to be false in the application.properties file of your project.

spring.devtools.restart.enabled = false

I hope this short tutorial on Spring Boot was helpful to you. If you want to learn more about building RESTful Web Services with Spring Boot and Spring MVC, please check the Spring Boot tutorials page. And if you enjoy learning by watching video lessons, look at the below list of video courses that teach Spring Boot.

Happy learning!

Leave a Reply

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