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.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.appsdeveloperblog.photoapp.api.users</groupId> <artifactId>PhotoAppApiUsers</artifactId> <version>0.0.1-SNAPSHOT</version> <name>PhotoAppApiUsers</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</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-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
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!