How to hot deploy a Spring Boot application
Spring Boot allows to automatically reload an application when the source code is updated.
This feature can be particularly useful when you are in development phase and you don’t want to manually restart your instance.
JUnit tests remain the preferred method to test the new code features but the reload can simplify the development workflow of small application or web services.
Here you can find the official documentation:
Add the DevTools to your project
Here the maven dependency to add the DevTools to your Spring Boot project.
When Spring Boot recognizes that the DevTools are in the path, it activates automatically the features.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
Spring smartly disable the extra features if you are running in ‘production mode’ to avoid the extra overhead.
Main features
- caching is disabled e.g. static resources
The static resources will be automatically updated without the need to restart the application. This is very useful if you are developing a frontend application and your web files are changing very often. Spring will simply reload them at every call avoiding the need to recompile and redeploy the application.
- Reload of the application
Spring will create 2 classloaders, 1 with external libraries and 1 for the project classes. The second classloader will be restarted every time a project class is updated or the developer trigger the restart with the spring.devtools.restart.trigger-file
option.
How the reload works
The Spring Boot application starts with 2 classloaders
After the compilation the classloaders with your project classes is destroyed and recreated with the new version of the classes.
This has some benefits compared to a complete application reload and some limitations compared to commercial solutions.
But hey … in my opinion it’s a great option that comes out of the box of the Spring framework and it’s comparable to the reload feature of JakartaEE / JavaEE application servers.
Disable the restart
If the restart is counter-productive because slow down your machine or is disturbing your development flow you can disable in application.properties
with the property spring.devtools.restart.enabled
.
Deep dive
The documentation page of the DevTool gives you many other options and information. I recommend to give it a quick read to see what else you can use in your project. Here we wanted to show quickly how to implement it in a small demo.