Spring Boot 3 and Spring Framework 6.0 – What’s New – Spring Boot 3和Spring Framework 6.0 – What’s New

最后修改: 2022年 8月 2日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

With only a short while until the release of Spring Boot 3, now seems like a good time to check out what’s new.

距离Spring Boot 3的发布只有很短的时间了,现在似乎是查看新内容的好时机。

2. Java 17

2.爪哇 17

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

虽然之前已经有对Java 17的支持,但这个LTS版本现在得到了基线。

When migrating from LTS version 11, Java developers will benefit from new language features. Since Java itself isn’t the topic of this article, we’ll only name the most important new features for Spring Boot developers. We can find additional details in the separate articles for Java 17, 16, 15, 14, 13, and 12.

从LTS版本11迁移时,Java开发者将从新的语言特性中受益。由于Java本身并不是本文的主题,我们只列举对Spring Boot开发人员来说最重要的新功能。我们可以在针对Java的单独文章中找到更多细节1716151413以及12

2.1. Records

2.1.记录

Java records (JEP 395, see Java 14 Record Keyword) were intended to be used as a quick 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).

Java记录(JEP 395,见Java 14 Record Keyword)旨在被用作创建数据载体类的快速方法,即其目标是简单地包含数据并在模块之间携带数据的类,也被称为POJOs(Plain Old Java Objects)和DTO(Data Transfer Objects)。

We can easily create immutable DTOs:

我们可以轻松地创建不可变的DTO。

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

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

目前,我们在将其与Bean Validation相结合时需要谨慎,因为构造函数参数不支持验证约束,例如在JSON反序列化(Jackson)时创建实例,并将其作为参数放入控制器的方法。

2.2. Text Blocks

2.2 文本块

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

通过JEP 378,现在可以创建多行文本块,而不需要在换行时连接字符串。

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

2.3. Switch Expressions

2.3.切换表达式

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:

Java 12 引入了开关表达式(JEP 361),它(像所有表达式一样)评估一个单一的值,并且可以在语句中使用。我们现在可以使用switchcase-结构,而不是将嵌套的ifelse-操作符(?

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

2.4.模式匹配

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

模式匹配是在Project Amber中阐述的,并在Java语言中找到了它们的方法。在Java语言中,它们可以帮助简化instanceof评估的代码。

We can use them directly with instanceof:

我们可以用instanceof直接使用它们。

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

We can also use it within a switchcase statement:

我们也可以在switchcase语句中使用它。

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

2.5.密封的类和接口

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.

我们可以在Java中的密封类和接口中找到更多细节。

3. Jakarta EE 9

3.雅加达EE 9

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

最重要的变化可能是从Java EE跳到Jakarta EE9,包的命名空间从javax.*变为jakarta.*。因此,只要我们直接使用来自Java EE的类,就需要调整我们代码中的所有导入。

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

例如,当我们在Spring MVC控制器中访问HttpServletRequest对象时,我们需要替换。

import javax.servlet.http.HttpServletRequest;

with:

与。

import jakarta.servlet.http.HttpServletRequest;

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

当然,我们不必经常使用Servlet API的类型,但如果我们使用Bean验证和JPA,这就不可避免了。

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

当我们使用依赖于Java/Jakarta EE的外部库时,我们也应该注意到这一点(例如,我们必须使用Hibernate Validator 7+,Tomcat 10+,和Jetty 11+)。

4. Further Dependencies

4.进一步的依赖性

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

Spring Framework 6和Spring Boot 3需要以下最低版本。

5. Big Points

5.大点数

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

5.1.本地可执行文件

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

构建原生可执行文件并将其部署到GraalVM中,会获得更高的优先权。因此,Spring Native计划正在转移到Spring本身

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

对于AOT的生成,不需要包含单独的插件,我们只需使用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).

Native Hints也将成为Spring核心的一部分。用于该的测试基础设施将在里程碑5(v6.0.0-M5)中提供。

5.2. Observability

5.2.可观察性

With Spring 6, there was an Observability initiative that ended up with a new Micrometer Observation API and with the former Spring Cloud Sleuth project migrated to Micrometer Tracing. This is more for efficiently recording application metrics with Micrometer, and implementing tracing through providers, such as OpenZipkin or OpenTelemetry.

随着 Spring 6 的推出,出现了一项可观察性计划,该计划最终产生了新的 Micrometer 观察 API,并且以前的 Spring Cloud Sleuth 项目被迁移到 Micrometer 追踪 。这更多地是为了用Micrometer有效地记录应用程序的指标,并通过供应商实现跟踪,例如OpenZipkinOpenTelemetry

There’s auto-configuration for all of these in Spring Boot 3, and the Spring projects are working on instrumenting themselves using the Observation API.

在Spring Boot 3中,所有这些都有自动配置,而且Spring项目正在努力使用Observation API进行自我检测。

6. Smaller Changes in Spring Web MVC

6.Spring Web MVC中较小的变化

One of the most important new features is the support for RFC7807 (Problem Details Standard). Now we won’t need to include separate libraries, like Zalando Problem.

最重要的新功能之一是对RFC7807(问题细节标准)的支持。现在我们将不需要包括单独的库,如Zalando问题

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

另一个较小的变化是,HttpMethod不再是一个枚举,而是一个类,允许我们为扩展的HTTP方法创建实例,例如那些由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).

至少放弃了一些过时的基于servlet的集成,如Commons FileUpload(我们应该使用StandardServletMultipartResolver 进行多部分文件上传)、Tiles和FreeMarker JSP支持(我们应该使用FreeMarker模板视图代替)。

7. Migrating Projects

7.迁移项目

There are a few 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 will be 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 isn’t yet released, we can try the current milestone to test the migration

8. Conclusion

8.结语

As we’ve learned, 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’ll benefit the most from the upcoming major release.

正如我们所了解的,迁移到Spring Boot 3和Spring 6也将是向Java 17和Jakarta EE 9的迁移。如果我们高度重视可观察性和原生可执行文件,我们就会从即将到来的主要版本中受益最多。

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

像往常一样,所有的代码都可以在GitHub上找到