Spring Top

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

>> LEARN SPRING

1. Overview

It's only 3 months until the release of Spring Boot 3. Spring Framework 6.0 will probably appear shortly before Spring Boot 3. So now is a good time to check out what's new.

2. Java 17

While there was already support for Java 17 before, this LTS version now gets the baseline.

When migrating from LTS version 11, Java developers benefit from new language features. Because in this article, Java itself is not the topic, let's only name the most important new features for Spring Boot developers. We could find any details in separate articles for Java 17, 16, 15, 14, 13, and 12.

2.1. Records

Java records (JEP 395, see Java 14 Record Keyword) were introduced with the intention to be used as a fast way to create data carrier classes, i.e. the classes whose objective is to simply contain data and carry it between modules, also known as POJOs (Plain Old Java Objects) and DTOs (Data Transfer Objects).

We can easily create immutable DTOs:

public record Person (String name, String address) {}

Currently, we need to be careful when combining them with Bean Validation because validation constraints are not supported on constructor arguments, e.g. when the instance is created on JSON deserialization (Jackson) and put into a controller's method as a parameter.

2.2. Text Blocks

With JEP 378, it is now possible to create multi-line text blocks without the need to concatenate strings on line breaks:

String textBlock = """
Hello, this is a
multi-line
text block.
""";

2.3. Switch Expressions

Java 12 introduced switch expressions (JEP 361), which (like all expressions) evaluate a single value, and can be used in statements. Instead of combining nested ifelse-operators (?:), we can now use a switchcase-construct:

DayOfWeek day = DayOfWeek.FRIDAY;
int numOfLetters = switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> 6;
    case TUESDAY                -> 7;
    case THURSDAY, SATURDAY     -> 8;
    case WEDNESDAY              -> 9;
};

2.4. Pattern Matching

Pattern Matchings were elaborated in Project Amber and found their way to the Java Language. In the case of the Java Language, they can help to simplify the code for instanceof evaluations.

We can use them directly with instanceof:

if (obj instanceof String s) {
    System.out.println(s.toLowerCase());
}

We can also use it within a switchcase statement:

static double getDoubleUsingSwitch(Object o) {
    return switch (o) {
        case Integer i -> i.doubleValue();
        case Float f -> f.doubleValue();
        case String s -> Double.parseDouble(s);
        default -> 0d;
    };
}

2.5. Sealed Classes and Interfaces

Sealed classes can limit inheritance by specifying allowed subclasses:

public abstract sealed class Pet permits Dog, Cat {}

We can find more details in Sealed Classes and Interfaces in Java.

3. Jakarta EE 9

This most important breaking change might be the jump from Java EE to Jakarta EE9, where the package namespace changed from javax.* to jakarta.*. So we need to adjust all imports in our code, whenever we use classes from Java EE directly.

For example, when we access the HttpServletRequest object within your Spring MVC Controller, we need to replace:

import javax.servlet.http.HttpServletRequest;

with:

import jakarta.servlet.http.HttpServletRequest;

Of course, we do not have to use the types of the Servlet API very often, but this is unavoidable if we use bean validation and JPA.

And we should be aware when we use external libraries that depend on Java/Jakarta EE too (e.g. we have to use Hibernate Validator 7+, Tomcat 10+ and Jetty 11+).

4. Further Dependencies

Spring Framework 6 and Spring Boot 3 need the following minimum versions:

5. Big Points

Two overarching topics have received particular attention: Native Executables and Observability. Overarching means, that:

  • the Spring Framework introduces core abstractions
  • the portfolio projects consistently integrate with them
  • Spring Boot provides auto-configuration

5.1. Native Executables

Building native executables and deploying them to GraalVM gets a higher priority. So the Spring Native initiative is moving into Spring proper.

For AOT generation, there is no need to include separate plugins, we could just use a new goal of the spring-boot-maven-plugin:

mvn spring-boot:aot-generate

Native Hints will also be part of the Spring core. Testing infrastructure for this will be available with Milestone 5 (v6.0.0-M5).

5.2. Observability

Spring 6 introduces Spring Observability – a new project that builds on Spring Cloud Sleuth. It is more for efficiently recording application metrics with Micrometer and implementing tracing through providers such as OpenZipkin or OpenTelemetry.

Spring Observability scores over previous agent-based observability, as it works seamlessly in natively compiled Spring applications to more effectively provide better information.

6. Smaller Changes in Spring Web MVC

One of the most important new features is the support for RFC7807 (Problem Details Standard). There will be no need to include separate libraries like Zalando Problem.

Another smaller change is that HttpMethod is no longer an enum but a class that allows creating instances for extended HTTP methods, e.g. those defined by WebDAV :

HttpMethod lock = HttpMethod.valueOf("LOCK");

At least some outdated servlet-based integrations are dropped, like Commons FileUpload (we should use StandardServletMultipartResolver  for multipart file uploads), Tiles, and FreeMarker JSP support (we should use FreeMarker template views instead).

7. Migrating Projects

There are some hints for project migration that we should know. The recommended steps are:

  1. Migrate to Spring Boot 2.7 (when Spring Boot 3 is released, there will be a migration guide based on Spring Boot 2.7)
  2. Check for deprecated code usage and legacy config file processing – it is removed with the new major release
  3. Migrate to Java 17
  4. Check third-party projects to have Jakarta EE 9 compatible releases
  5. Since Spring Boot 3 is not released, we can try the current milestone to test the migration

8. Conclusion

As we have seen, migrating to Spring Boot 3 and Spring 6 will be a migration to Java 17 and Jakarta EE 9 too. If we attach great importance to observability and native executables, we will benefit the most from the upcoming major release.

As always, all the code is available over on GitHub.

Spring bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> THE COURSE
Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments