Working on microservices can be a grind — make a change, restart the app, wait a bit to check the result. Good news: DevTools takes some of that routine off your plate! This tool was built specifically to speed up the development loop.
What is DevTools?
DevTools is a set of Spring Boot tools that includes:
- Automatic application restart when code changes.
- Live reload — the browser refreshes right after changes.
- Handy settings you can disable in production to keep development fast.
Why is DevTools useful for microservices? Here's a simple (almost poetic) truth: microservices = lots of changes = more time spent testing and debugging. DevTools helps you:
- Avoid manually restarting the app after every change.
- Speed up testing and debugging changes in your REST API or other microservice components.
- Save time by cutting out repetitive, boring steps.
Installing and configuring DevTools
To enable DevTools in your project, add the following dependency to your pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
If you prefer Gradle:
implementation 'org.springframework.boot:spring-boot-devtools'
Note: DevTools is meant for development only. It’s automatically disabled in the prod environment, so you don’t need to worry about production performance.
Now restart the app (yeah, irony 😅, but just this once, I promise), and you're good to go!
How does it work?
- Start your application.
- Make some change in the code (for example, tweak some text in a REST controller).
- Save the change.
- DevTools restarts the app automatically, and you immediately see the updated result!
For an even smoother experience, you can install a browser plugin (for example, Live Reload for Chrome) — this lets the page refresh automatically after each change.
Spring Boot profiles: configuring apps for different environments
Spring profile is a way to switch configurations depending on the app's current environment. For example:
- Development (
dev) — use the embedded H2 database. - Testing (
test) — use test-specific configs. - Production (
prod) — real settings with a production database.
Spring Boot lets you add profiles to simplify managing configs across different stages of development.
Configuring profiles in application.yml
Let's create an example application.yml to define different configs per environment.
# General settings (default)
server:
port: 8080
spring:
datasource:
url: jdbc:h2:mem:testdb
driver-class-name: org.h2.Driver
username: sa
password: password
# Configuration for the dev profile
---
spring:
config:
activate:
on-profile: dev
server:
port: 8081
logging:
level:
root: DEBUG
# Configuration for the prod profile
---
spring:
config:
activate:
on-profile: prod
spring:
datasource:
url: jdbc:mysql://prod-db-server/database
username: prod_user
password: prod_password
logging:
level:
root: INFO
Here’s how it works:
- The
---section marks the start of a new profile block. - The property
spring.config.activate.on-profileactivates the profile.
Selecting a profile
Activate a profile by setting the environment variable SPRING_PROFILES_ACTIVE:
export SPRING_PROFILES_ACTIVE=dev
Or set the profile on the command line when running the app:
java -Dspring.profiles.active=prod -jar your-app.jar
If you use an IDE, specify the profile in the run configuration.
Practical steps
- Create two profiles —
devandprod. - Configure
devto use H2 andprodto use MySQL. - Activate
devfor local development. Make sure everything works. - Switch to
prodand verify the connection to the real database.
How DevTools + profiles make development easier
Microservices evolve quickly, and handling configs is unavoidable. Instead of editing configs manually for every task, profiles save us a ton of time.
And DevTools lets us quickly verify changes without manual restarts. The result is a developer-friendly workflow — less stress, more efficiency.
Digging into DevTools: features and limitations
With DevTools, development is more than just automatic restarts. Here are some other neat features:
- Disable template caching:
- Add to
application.yml:spring.thymeleaf.cache: false - This lets you see HTML template changes immediately.
- Add to
- Extra settings for live reload:
- DevTools only restarts the app when files in
src/mainchange. - Files in
src/testare not watched — this prevents tests from triggering app restarts.
- DevTools only restarts the app when files in
Remember: DevTools is a development tool. Don’t try to use it in production. That’s almost as bad as reinventing the wheel instead of using a proven library.
Summary
Now you know how to:
- Set up Spring Boot DevTools to speed up development.
- Use profiles to flexibly configure microservices for different environments.
- Activate the right configuration depending on the environment you’re in.
Knowing DevTools and profiles will help not just with microservices, but with any app. And remember: more automation = less busywork, which means more time for the fun stuff (like writing witty code comments or reading docs 😉).
GO TO FULL VERSION