Spring Boot DevTools is a module made to speed up and improve the development experience for Spring Boot apps. Its main job is to cut down the time spent restarting your app and help automate boring stuff like reloading after changes, cache tweaks, and even refreshing the browser.
Main features of Spring Boot DevTools:
- Automatic application restart when code changes.
- Cache disabling so development feels faster (for example, so changes in Thymeleaf templates or data show up immediately).
- LiveReload for automatically refreshing the browser when the project changes.
DevTools is a lifesaver for developers tired of manually restarting apps after every small change. It's like having a "faithful compiler-bot" that restarts your server for you. Nice, right?
Adding Spring Boot DevTools
Let's start small. To use DevTools, you just need to add the dependency to your project.
If you're using Maven, add the following dependency to your pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
For Gradle, do the same in build.gradle:
implementation 'org.springframework.boot:spring-boot-devtools'
DevTools is meant for development only. It won't be active in production because its dependencies are marked as runtime or developmentOnly. Spring automatically disables DevTools in production mode.
Automatic application restart
What's happening under the hood?
Spring Boot DevTools uses the Spring Loaded library or the Restart ClassLoader to watch for changes in your project. When it detects changes, DevTools automatically reloads only the changed code without restarting the entire application context. That speeds up your development loop by orders of magnitude.
How does it work?
If everything is set up correctly, you don't need to do anything else. Just change any file (Java code, settings in application.properties, template files, etc.) and save it. The app will restart automatically.
For example, you might have a controller like this:
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, DevTools!";
}
}
Now change the return value of sayHello() to something else, like "Hello, Spring Boot!". When you save the file, DevTools will update the app automatically and the new response will be available without stopping and restarting the server.
What to keep in mind?
- DevTools reloads classes located in
src/main/java, but ignores changes in dependencies (for example, insrc/test/javaor libraries pulled in via Maven/Gradle). - If you change embedded server settings (like the port or HTTPS settings), you may still need to restart the app manually.
4. LiveReload: automatic browser refresh
LiveReload is an extra DevTools feature that automatically refreshes the browser when you change code or project files. It's super handy when you're working on UI or templates (for example, with Thymeleaf).
To use LiveReload you need to install the browser extension. Here's a link to the official LiveReload site where you can grab the extension for your browser.
How to enable LiveReload?
LiveReload is enabled in DevTools by default. After you start the app, make sure the LiveReload extension in your browser is active. Now, if you change a Thymeleaf template, the page will update automatically.
Try this Thymeleaf template:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello DevTools</title>
</head>
<body>
<h1 th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
Now if you change something in the template, for example change h1 to h2, the browser will immediately show the new version without you manually refreshing the page.
Disabling DevTools
If you're working on large projects, automatic restarts can get expensive. In those cases you can disable DevTools partially or completely.
Disable automatic restart
Edit application.properties:
spring.devtools.restart.enabled=false
Disable LiveReload
If you don't want LiveReload, add this to application.properties:
spring.devtools.livereload.enabled=false
Now the app won't refresh the browser automatically anymore.
Configuring DevTools for convenience
You can tune DevTools to suit your workflow. Here are some useful settings:
| Parameter | Description |
|---|---|
spring.devtools.restart.enabled |
Enable/disable automatic restart |
spring.devtools.livereload.enabled |
Enable/disable LiveReload |
spring.devtools.restart.additional-paths |
Additional paths to watch |
spring.devtools.restart.exclude |
Exclude files or directories from watching (for example, logs) |
spring.devtools.remote.secret |
Protection for remote access via DevTools |
Example using additional-paths and exclude
Say you have a directory src/main/resources/static where static files live. You want DevTools to watch that folder and restart the app when files change. Add this to application.properties:
spring.devtools.restart.additional-paths=src/main/resources/static
spring.devtools.restart.exclude=*.log
Handy tips and common pitfalls
Tips
- Make sure DevTools is added only to development dependencies. Enabling DevTools in production can introduce unnecessary security risks.
- If you're working in a team, ensure everyone uses consistent DevTools settings to avoid surprises.
- Use DevTools together with LiveReload if you're actively working on the UI. It speeds up development a lot.
Common issues
Sometimes DevTools may not restart the app. Check the following:
- Are there errors in the IDE console? Some libraries can conflict with DevTools.
- Make sure you're running in development mode. DevTools is inactive in production.
- If changes don't apply, ensure they're made under the
src/maindirectory.
How DevTools helps in practice
Spring Boot DevTools is a must-have for active developers. It's especially useful if:
- You're constantly modifying controllers, services, or templates.
- You're doing rapid prototyping and every second counts.
- You plan to integrate complex UIs with the backend.
With DevTools your workflow becomes not only faster but also easier, letting you focus on the fun part — writing code.
GO TO FULL VERSION