Sub topic 14
Next steps: DevTools and Actuator
This chapter covers two optional modules that make a Spring Boot project easier to work on and to operate: Spring Boot DevTools and Spring Boot Actuator. It assumes you can already generate a project with Spring Initializr, run it with ./mvnw spring-boot:run, and call the /hello endpoint from the earlier chapters. You should also know how a Maven <dependency> entry works and how application.properties overrides defaults.
Both modules are added through starters and configured through auto-configuration, so nothing here introduces a new mechanism. DevTools helps during development. Actuator helps once the application is running, whether on your laptop or on a server.
Spring Boot DevTools
DevTools shortens the edit-and-see loop. It is a separate dependency, not part of spring-boot-starter-web. Add it to the <dependencies> block of pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>The two settings matter:
runtimescope keeps DevTools out of the compile classpath, so your code cannot depend on it by accident.optionalstops the dependency from passing to other projects that depend on yours.
What DevTools does
DevTools provides four features:
- Automatic restart. When a file on the classpath changes, DevTools restarts the application.
- LiveReload. It runs a LiveReload server on port 35729. A browser extension can use it to refresh the page after a restart.
- Development-friendly defaults. For example, it turns off template caching, so edited templates show up without a restart.
- Remote support. It can be configured to work against a remote application. This is rarely needed, and it is a security risk if misused.
Why restarts are fast
DevTools uses two classloaders. Your own classes (the ones that change) load into a restart classloader. Third-party libraries, which rarely change, load into a base classloader. On a change, DevTools throws away the restart classloader and creates a new one. It does not reload the libraries or start a new JVM. This is usually much faster than stopping and starting the application by hand.
Common errors with DevTools
- Nothing restarts when you save. DevTools watches the compiled classpath, not your source files. In IntelliJ IDEA you must build the project (or enable automatic build). In Eclipse and VS Code, saving normally compiles. With plain Maven, run
./mvnw compilein a second terminal whilespring-boot:runis active. - DevTools seems to do nothing with
java -jar. This is intended. DevTools detects a fully packaged application and disables itself. By default the repackaged JAR also leaves DevTools out. - State disappears after each restart. Beans, caches, and in-memory data are rebuilt. That is normal. If you rely on a session surviving restarts, DevTools is not the problem. Your design is.
- A static resource change causes an unwanted restart. By default, changes under
/staticand/publicdo not trigger a restart. You can adjust this withspring.devtools.restart.exclude.
You can turn restart off while keeping the other features:
spring.devtools.restart.enabled=falseSpring Boot Actuator
Actuator adds production-ready endpoints that report on a running application: health, metrics, configuration, mappings, and more. Add the starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>Restart the application and call the health endpoint. The examples assume port 8080. If you set server.port=8081 earlier, use that port.
curl http://localhost:8080/actuator/healthThe response is:
{"status":"UP"}Actuator groups its features into endpoints, each with an ID such as health or metrics. Each endpoint can be enabled or disabled, and separately exposed over HTTP or JMX. Only health is exposed over HTTP by default.
Commonly used endpoints
| Endpoint ID | What it shows | Exposed over HTTP by default |
|---|---|---|
| `health` | Whether the application and its dependencies are up | Yes |
| `info` | Arbitrary application information you provide | No |
| `metrics` | JVM, HTTP, and custom metrics | No |
| `mappings` | Every request mapping, including your `/hello` route | No |
| `beans` | Every bean in the application context | No |
| `conditions` | Which auto-configuration classes applied, and why | No |
| `env` | Properties and property sources | No |
| `loggers` | Log levels, which can be changed at runtime | No |
The conditions endpoint is the runtime version of the auto-configuration report. When you want to know why Boot created, or did not create, a bean, it gives the answer. Starting the application with debug=true prints the same report to the console.
Exposing endpoints
Control exposure in application.properties:
management.endpoints.web.exposure.include=health,info,metrics,mappings
management.endpoint.health.show-details=always
management.info.env.enabled=true
info.app.name=demo
info.app.description=Spring Boot tutorial applicationEach line does one job:
exposure.includelists the endpoints reachable over HTTP. A value of*exposes all of them.show-details=alwaysadds the status of each health component to the response. The alternatives arenever(the default) andwhen-authorized.management.info.env.enabled=truelets theinfoendpoint publish properties that start withinfo.. This contributor is off by default.
After a restart, curl http://localhost:8080/actuator/info returns:
{"app":{"name":"demo","description":"Spring Boot tutorial application"}}To read one metric, name it in the path:
curl http://localhost:8080/actuator/metrics
curl http://localhost:8080/actuator/metrics/jvm.memory.usedThe first call lists the available metric names. The second returns the value and its measurement unit for one name.
Health indicators
The health endpoint combines the results of many HealthIndicator beans. Boot registers indicators automatically when the matching technology is on the classpath: a DataSource adds a database check, and disk space is checked by default. This is auto-configuration at work again.
You can add your own. Define a bean that implements HealthIndicator:
package com.example.demo;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class GreetingHealthIndicator implements HealthIndicator {
@Override
public Health health() {
return Health.up().withDetail("greeting", "available").build();
}
}Spring names the component after the bean, with the HealthIndicator suffix removed. With show-details=always, the response lists it as greeting. Overall status is DOWN if any component is DOWN. A real indicator would test something, such as a call to a downstream service, and return Health.down() when it fails.
Moving Actuator to its own port
You can serve management endpoints on a separate port, which makes it easier to block them at the network level:
management.server.port=9090The endpoints then answer at http://localhost:9090/actuator/health, and the main application port no longer serves them.
Common errors with Actuator
/actuator/metricsreturns 404. The endpoint is not exposed. Checkmanagement.endpoints.web.exposure.include.inforeturns{}. Noinfo.*properties exist, ormanagement.info.env.enabledis nottrue.- Health shows only
{"status":"UP"}. Details are hidden by default. Setshow-details. - Health is
DOWNafter you add a database starter. The database health indicator found no working connection. This is the indicator doing its job. - Endpoints return 401 or 403. Spring Security is on the classpath and protects them. Configure access rules instead of disabling security.
Choosing what to add
| Need | Use | Where it belongs |
|---|---|---|
| Faster edit-and-restart loop | DevTools | Development machine only |
| Health check for a load balancer or orchestrator | Actuator `health` | All environments |
| Visibility into metrics and configuration | Actuator `metrics`, `mappings`, `conditions` | Protected, with limited exposure |
| Find out why a bean was or was not created | `conditions` endpoint or `debug=true` | Development and troubleshooting |
Where to go next
You now have the full path from the earlier chapters: generate a project, read its structure and build file, run it, add an endpoint, configure it, and package it. DevTools makes that loop quicker, and Actuator makes the running result observable. Natural follow-ups are adding a data layer with Spring Data, writing tests with spring-boot-starter-test, securing endpoints with Spring Security, and sending Actuator metrics to a monitoring system through Micrometer.
Exercise
You add spring-boot-starter-actuator and start the application, but curl http://localhost:8080/actuator/metrics returns 404 while /actuator/health works. What is the cause, and which property fixes it?
Show answer
Only the health endpoint is exposed over HTTP by default, so metrics is not reachable. Add it to the exposure list, for example management.endpoints.web.exposure.include=health,metrics, then restart the application.
Exercise
You added DevTools, edited HelloController in IntelliJ IDEA, and saved the file, but the application did not restart. Give one likely reason and one fix.
Show answer
DevTools watches the compiled classpath, not source files, and IntelliJ IDEA does not compile on save by default. Build the project (Build > Build Project) or enable automatic build, and DevTools will detect the changed class and restart.
Exercise
Why is DevTools declared with <scope>runtime</scope> and <optional>true</optional>, and what happens to it when you run the packaged JAR with java -jar?
Show answer
Runtime scope keeps it off the compile classpath, and optional stops it from being passed on to projects that depend on yours. When the application runs as a packaged JAR, DevTools treats it as a production run and disables itself. By default the repackaged JAR also excludes it.