Spring Boot DevTools is a valuable tool for speeding up the development process by enabling features like automatic application restart and live reload of web content. It is specifically designed to improve the development experience by reducing the time required to restart the application during testing and debugging.
Here is a quick guide to using Spring Boot DevTools for faster development and live reload:
1. Add DevTools Dependency
To use Spring Boot DevTools, include it in your pom.xml
(Maven) or build.gradle
(Gradle) file.
Using Maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
Using Gradle:
implementation 'org.springframework.boot:spring-boot-devtools'
2. Automatic Restart
Spring Boot DevTools triggers an automatic application restart whenever files in the classpath are modified. It uses two classloaders—one for the static resources and one for the application classes—enabling a fast reload experience.
- Restart Triggering: Files located under
/src/main/resources/
,/src/main/java/
, or any other classpath resources automatically restart the application upon modification. - Excluding Certain Files from Restart: You can exclude specific file patterns using the property:
spring.devtools.restart.exclude=static/**,public/**
3. Live Reload (Optional with Browser)
Spring Boot DevTools integrates with LiveReload, so front-end changes (e.g., HTML, CSS, or JavaScript) trigger an automatic browser reload.
Steps for LiveReload:
- Install a LiveReload extension in your browser (available for Chrome, Firefox, etc.).
- DevTools will automatically enable LiveReload if the extension is active.
If you want to disable the LiveReload capability, use the following property:
spring.devtools.livereload.enabled=false
4. Property Defaults in Development vs. Production
DevTools provides sensible defaults for development environments that are different from production. For instance:
- Caching is disabled for templates (e.g., Thymeleaf, FreeMarker, etc.).
- Hibernate auto-detection for changes in the database schema is enabled.
If you want to customize DevTools properties, you can use a dedicated application-dev.properties
profile.
5. How to Enable Conditional DevTools Behavior
To avoid shipping DevTools to production, mark it as optional=true
in Maven or use a developmentOnly
configuration in Gradle.
Example for Gradle:
developmentOnly 'org.springframework.boot:spring-boot-devtools'
6. Disable Restart in Specific Scenarios
If you don’t want restart functionality during your development, you can disable it with the property:
spring.devtools.restart.enabled=false
7. Trigger a Manual Restart
If you want to trigger a restart manually during development, you can:
- Add or remove files in the
/META-INF/spring-devtools.properties
directory to trigger a restart.
8. Example Use Case: Thymeleaf or Front-End Modification
If you’re using Thymeleaf templates in a Spring Boot application for the web frontend:
- Modify an HTML file under
/src/main/resources/templates
. - The browser will refresh automatically (if LiveReload is enabled). You’ll instantly see your changes without manually restarting or refreshing.
Important Notes:
- Spring Boot DevTools is solely for development purposes and should not be packaged into your production build.
- If you’re running in Docker or a cloud environment, ensure that file watchers are properly configured for file changes.
Incorporating Spring Boot DevTools provides a more efficient development experience by automating repetitive actions and making live coding efforts more seamless!