Spring Boot 3 with Java 17
Spring Boot 3 marks a significant milestone, requiring Java 17 as its baseline. This upgrade brings a host of new features, performance improvements, and a cleaner codebase, leveraging the latest advancements in the Java ecosystem. In this post, we'll explore what's new in Spring Boot 3 and how to effectively migrate or start new projects with Java 17.
Java 17 Baseline
The most notable change in Spring Boot 3 is the mandatory requirement of Java 17. This allows Spring Boot to fully embrace modern Java features like Records, Sealed Classes, and Pattern Matching for Switch, leading to more concise and expressive code.
// Example of a record in Spring Boot 3
public record Product(Long id, String name, double price) {}
// Using pattern matching for switch in a Spring controller
@GetMapping("/items/{item}")
public String getItemType(@PathVariable Object item) {
return switch (item) {
case Integer i -> "Integer item: " + i;
case String s -> "String item: " + s;
default -> "Unknown item type";
};
}
GraalVM Native Images Support
Spring Boot 3 introduces first-class support for GraalVM Native Images. This allows you to compile your Spring Boot applications into native executables, resulting in:
- Instant Startup Times: Native images start in milliseconds, significantly reducing cold start times in serverless and containerized environments.
- Reduced Memory Footprint: Native images consume significantly less memory compared to traditional JVM applications.
- Smaller Deployment Sizes: The compiled executable is often much smaller, simplifying deployment.
To build a native image, you can use the Spring Boot Maven or Gradle plugin with the `native` profile:
# Maven
./mvnw native:compile
# Gradle
./gradlew nativeCompile
Observability with Micrometer
Spring Boot 3 enhances observability by integrating Micrometer Tracing and Micrometer Observation. This provides a unified API for metrics, logging, and tracing, making it easier to understand the behavior of your applications in production.
You can now easily add custom observations to your code:
// Example of using Observation API
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
@Service
public class MyService {
private final ObservationRegistry observationRegistry;
public MyService(ObservationRegistry observationRegistry) {
this.observationRegistry = observationRegistry;
}
public String doSomething() {
return Observation.createNotStarted("my.service.operation", this.observationRegistry)
.lowCardinalityKeyValue("type", "example")
.highCardinalityKeyValue("details", "some-dynamic-value")
.observe(() -> {
// Your business logic here
System.out.println("Performing some operation...");
return "Operation completed";
});
}
}
HTTP Interfaces
Spring Boot 3 introduces HTTP Interfaces, a declarative way to define HTTP clients. This simplifies the creation of REST clients by allowing you to define an interface with annotations, and Spring will generate the implementation at runtime.
// Define an HTTP client interface
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.service.annotation.GetExchange;
public interface TodoClient {
@GetExchange("/todos/{id}")
Todo getTodoById(@PathVariable("id") Long id);
}
// Use it in your service
@Service
public class TodoService {
private final TodoClient todoClient;
public TodoService(TodoClient todoClient) {
this.todoClient = todoClient;
}
public Todo fetchTodo(Long id) {
return todoClient.getTodoById(id);
}
}
Jakarta EE 9+ Baseline
Spring Boot 3 has migrated from Java EE to Jakarta EE APIs (specifically Jakarta EE 9 and above). This means that packages like `javax.*` have been replaced with `jakarta.*`. While this is a breaking change, it aligns Spring Boot with the latest evolution of enterprise Java standards.
Conclusion
Spring Boot 3, with its Java 17 baseline and support for GraalVM Native Images, Micrometer Observability, and HTTP Interfaces, is a powerful release that pushes the boundaries of modern Java application development. It enables developers to build highly performant, resource-efficient, and observable applications with greater ease.
Upgrading to Spring Boot 3 is highly recommended to take advantage of these new capabilities and ensure your applications are future-proof.