加载中

Spring Framework 5.0 is the first major release of the Spring Framework since version 4 was released in December of 2013. Juergen Hoeller, Spring Framework project lead, announced the release of the first Spring Framework 5.0 milestone (5.0 M1) on July 28, 2016.

Now, almsot a year later, we are looking forward to Release Candidate 3 (RC3) to be released on July 18, 2017. This is expected to be the final release on the roadmap to the first GA (General Availability) release of Spring Framework 5.0.

Spring Framework 5.0 是自 2013年12月版本 4 发布之后 Spring Framework 的第一个主发行版。Spring Framework 项目的领导人 Juergen Hoeller 于 2016 年 7 月 28 日宣布了第一个 Spring Framework 5.0 里程碑版本(5.0 M1)。

现在,将近一年的时间过去以后,我们期盼已久的 RC3 版本将于 2017 年 7 月 18 日发行。这是路线图规划中 Spring Framework 5.0 首个 GA 发行版的最后一次发行。

I’m excited about the new features and enhancements in Spring Framework 5.0.

At a high level, features of Spring Framework 5.0 can be categorized into:

  • JDK baseline update.

  • Core framework revision.

  • Core container updates.

  • Functional programming with Kotlin.

  • Reactive Programming Model.

  • Testing improvements.

  • Library support.

  • Discontinued support.

从高层来看,Spring Framework 5.0 的功能可以分为:

  • JDK 基线更新

  • 核心框架修正

  • 核心容器更新

  • 含 Kotlin 在内的函数式编程

  • 响应式编程模型

  • 测试改进

  • 库支持

  • 中止支持

JDK Baseline Update for Spring Framework 5.0

The entire Spring framework 5.0 codebase runs on Java 8. Therefore, Java 8 is the minimum requirement to work on Spring Framework 5.0.

This is actually very significant for the framework. While as developers, we’ve been able to enjoy all the new features found in modern Java releases. The framework itself was carrying a lot of baggage in supporting deprecated Java releases.

The framework now requires a minimum of Java 8.

Originally, Spring Framework 5.0 was expected to release on Java 9. However, with the Java 9 release running 18+ months behind, the Spring team decided to decouple the Spring Framework 5.0 release from Java 9.

However, when Java 9 is released (expected in September of 2017), Spring Framework 5.0 will be ready.

Spring Framework 5.0 的 JDK 基线更新

整个 Spring framework 5.0 代码库运行于 Java 8 之上。因此 Spring Framework 5.0 对环境的最低要就就是 Java 8。

这一点实际上对框架而言非常重要。而作为开发者的我们而言,则已经能够去藉此来享受到现代 Java 发行版中的所有新特性了。而框架版本还背负这支持已经不被建议使用的 Java 发行版的任务。

现在,框架的最低要求是 Java 8.

Spring Framework 5.0 原来是计划在 Java 9 之上发行的。然后,在基于 Java 9 的发行版运作了超过 18 个月之后, Spring 团队决定将 Spring Framework 5.0 发行版同 Java 9 的绑定关系解除。

不过,在 Java 9 发布的时候(计划是2017年9月份), Spring Framework 5.0 会准备好的。

Core Framework Revision

The core Spring Framework 5.0 has been revised to utilize the new features introduced in Java 8. The key ones are:

  • Based on Java 8 reflection enhancements, method parameters in Spring Framework 5.0 can be efficiently accessed.

  • Core Spring interfaces now provide selective declarations built on Java 8 default methods.

  • @Nullable and @NotNull annotations to explicitly mark nullable arguments and return values. This enables dealing null values at compile time rather than throwing NullPointerExceptions at runtime.

On the logging front, Spring Framework 5.0 comes out of the box with Commons Logging bridge module, named spring-jcl instead of the standard Commons Logging. Also, this new version will auto detect Log4j 2.x, SLF4J, JUL ( java.util.logging) without any extra bridges.

Defensive programming also gets a thrust with the Resource abstraction providing the isFile indicator for the getFile method.

核心框架修订

核心的 Spring Framework 5.0 已经利用 Java 8 所引入的新特性进行了修订。比较关键的一些如下:

  • 基于 Java 8 的反射增强,  Spring Framework 5.0 中的方法参数可以更加高效的进行访问。

  • 核心的 Spring 接口现在提供基于Java 8 的默认方法构建的选择性声明。

  • 用 @Nullable 和 @NotNull 注解来显示表明可为空的参数和以及返回值。这样就够在编译的时候处理空值而不是在运行时抛出 NullPointerExceptions。

在日志记录方面, Spring Framework 5.0 带来了 Commons Logging 桥接模块的封装, 它被叫做 spring-jcl 而不是标准的 Commons Logging。当然,无需任何额外的桥接,新版本也会对 Log4j 2.x, SLF4J, JUL ( java.util.logging) 进行自动检测。

有了 Resourse 抽象所提供的 isFile 指示器以及 getFile 方法,防御式编程方法也得到了框架的推动。

Core Container Updates

Spring Framework 5.0 now supports candidate component index as an alternative to classpath scanning. This support has been added to shortcut the candidate component identification step in the classpath scanner.

An application build task can define its own META-INF/spring.components file for the current project. At compilation time, the source model is introspected and JPA entities and Spring Components are flagged.

Reading entities from the index rather than scanning the classpath does not have significant differences for small projects with less than 200 classes. However, it has significant impacts on large projects. Loading the component index is cheap. Therefore the startup time with the index remains constant as the number of classes increases. 

核心容器更新

Spring Framework 5.0 现在支持候选组件索引作为类路径扫描的替代方案。该功能已经在类路径扫描器中添加,以简化添加候选组件标识的步骤。

应用程序构建任务可以定义当前项目自己的 META-INF/spring.components 文件。在编译时,源模型是自包含的,JPA 实体和 Spring 组件是已被标记的。

从索引读取实体而不是扫描类路径对于小于 200 个类的小型项目是没有明显差异。但对大型项目影响较大。加载组件索引开销更低。因此,随着类数的增加,索引读取的启动时间将保持不变。

Loading the component index is cheap. Therefore the startup time with the index remains constant as the number of classes increases, while for a component scan, the startup time increases significantly.

What this means for us developers on large Spring projects, the startup time for our applications will be reduced significantly. While 20 or 30 seconds does not seem like much, when you’re waiting for that dozens or hundreds of times a day, it adds up. Using the component index will help with your daily productivity.

You can find more information on the component index feature on Spring’s Jira.

Now  @Nullable annotations can also be used as indicators for optional injection points. Using  @Nullable imposes an obligation on the consumers that they must prepare for a value to be null. Prior to this release, the only way to accomplish this is through Android’s Nullable, Checker Framework’s Nullable, and JSR 305’s Nullable.

加载组件索引的耗费是廉价的。因此当类的数量不断增长,加上构建索引的启动时间仍然可以维持一个常数, 不过对于组件扫描而言,启动时间则会有明显的增长。

这个对于我们处于大型 Spring 项目的开发者所意味着的,是应用程序的启动时间将被大大缩减。虽然 20 或者 30 秒钟看似没什么,但如果每天要这样登上好几百次,加起来就够你受的了。使用了组件索引的话,就能帮助你每天过的更加高效。

你可以在 Spring 的 Jira上了解更多关于组件索引的相关信息。

@Nullable 注解现在也可以被用来作为可选注入项的指示器。@Nullable 为对象使用方规定了一项义务,就是它们必须准备以为取值为 null 的值。在此次发布之前,实现这件事情的唯一方法就是通过 Android 的 NullableChecker Framework 的 Nullable, 以及 JSR 305 的 Nullable

Some other new and enhanced features from the release note are:

  • Implementation of functional programming style in GenericApplicationContext and AnnotationConfigApplicationContext.

  • Consistent detection of transaction, caching, and async annotations on interface methods.

  • XML configuration namespaces streamlined towards unversioned schemas.

发行说明中的其他一些新功能和增强功能包括:

  • 在 GenericApplicationContext 和 AnnotationConfigApplicationContext 中实现函数式编程风格。

  • 对接口方法的事务、缓存和异步注释的一致性检测。

  • 将 XML 配置命名空间简化为无版本化的模式。

Functional Programming With Kotlin

Spring Framework 5.0 introduces support for JetBrains Kotlin language. Kotlin is an object-oriented language supporting functional programming style. Kotlin runs on top of the JVM, but not limited to it.

With Kotlin support, developers can dive into functional Spring programming, in particular for functional Web endpoints and bean registration.

Kotlin runs on top of the JVM, but not limited to it. With Kotlin support, developers can dive into functional Spring programming, in particular for functional Web endpoints and bean registration.

In Spring Framework 5.0, you can write clean and idiomatic Kotlin code for Web functional API, like this.

{
("/movie" and accept(TEXT_HTML)).nest {
GET("/", movieHandler::findAllView)
GET("/{card}", movieHandler::findOneView)
}
("/api/movie" and accept(APPLICATION_JSON)).nest {
GET("/", movieApiHandler::findAll)
GET("/{id}", movieApiHandler::findOne)
}
}

For bean registration, as an alternative to XML or @Configuration and @Bean, you can now use Kotlin to register your Spring Beans, like this:

val context = GenericApplicationContext {
registerBean()
registerBean { Cinema(it.getBean()) }
}

Click here to learn about my new Spring Framework 5 course!

使用 Kotlin 进行函数式编程

Spring Framework 5.0 引入了对 JetBrains Kotlin 语言的支持。Kotlin 是一种支持函数式编程编程风格的面向对象语言。Kotlin 运行在 JVM 之上,但运行环境并不限于 JVM。

有了对 Kotlin 的支持,开发者可以进行深度的函数式 Spring 编程,特别是在函数式 Web 端点以及 Bean 注册这些方面。

在 Spring Framework 5.0 中, 你可以为 WEB 的函数式 API 编写干净且地道的 Kotlin 代码,就像下面这样:

{
("/movie" and accept(TEXT_HTML)).nest {
GET("/", movieHandler::findAllView)
GET("/{card}", movieHandler::findOneView)
}
("/api/movie" and accept(APPLICATION_JSON)).nest {
GET("/", movieApiHandler::findAll)
GET("/{id}", movieApiHandler::findOne)
}
}

对于 Bean 的注册,作为 XML 或者 @Configuration 以及 @Bean 的替代办法, 现在你可以使用 Kotlin 来注册 Spring Bean了,就像下面这样:

val context = GenericApplicationContext {
registerBean()
registerBean { Cinema(it.getBean()) }
}

点击这里了解我的新的 Spring Framework 5 课程的相关信息!

Reactive Programming Model

An exciting feature in this Spring release is the new reactive stack Web framework. Being fully reactive and non-blocking, this stack is suitable for event-loop style processing that can scale with a small number of threads.

Being fully reactive and non-blocking, this Spring Framework 5.0 is suitable for event-loop style processing that can scale with a small number of threads.

Reactive Streams is an API specification developed by engineers from Netflix, Pivotal, Typesafe, Red Hat, Oracle, Twitter, and Spray.io. This provides a common API for reactive programming implementations to implement. Much like JPA for Hibernate. Where JPA is the API, and Hibernate is the implementation.

The Reactive Streams API is officially part of Java 9. In Java 8, you will need to include a dependency for the Reactive Streams API specification.

响应式编程模型

此次 Spring 发行版本的一个激动人心的特性就是新的响应式堆栈 WEB 框架。这个堆栈完全的响应式且非阻塞,适合于事件循环风格的处理,可以进行少量线程的扩展。

Reactive Streams 是来自于 Netflix, Pivotal, Typesafe, Red Hat, Oracle, Twitter 以及 Spray.io 的工程师特地开发的一个 API。它为响应式编程实现的实现提供一个公共的 API,好实现 Hibernate 的 JPA。这里 JPA 就是这个 API, 而 Hibernate 就是实现。

Reactive Streams API 是 Java 9 的官方版本的一部分。在 Java 8 中, 你会需要专门引入依赖来使用 Reactive Streams API。

Streaming support in Spring Framework 5.0 is built upon Project Reactor, which implements the Reactive Streams API specification.

Spring Framework 5.0 has a new spring-webflux module that supports reactive HTTP and WebSocket clients. Spring Framework 5.0 also provides support for reactive web applications running on servers which includes REST, HTML, and WebSocket-style interactions.

There are two distinct programming models on the server-side in spring-webflux:

  • Annotation-based with @Controller and the other annotations of Spring MVC.

  • Functional style routing and handling with Java 8 lambda.

With Spring Webflux, you can now create WebClient, which is reactive and non-blocking as an alternative to RestTemplate.

A WebClient implementation of a REST endpoint in Spring 5.0 is this.

WebClient webClient = WebClient.create();
Mono person = webClient.get()
.uri("http://localhost:8080/movie/42")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.then(response -> response.bodyToMono(Movie.class));

While the new WebFlux module brings us some exciting new capabilities, traditional Spring MVC is still fully supported in Spring Framework 5.0.

Spring Framework 5.0 对于流式处理的支持依赖于 Project Reactor 来构建, 其专门实现了 Reactive Streams API。

Spring Framework 5.0 拥有一个新的 spring-webflux 模块,支持响应式 HTTP 和 WebSocket 客户端。Spring Framework 5.0 还提供了对于运行于服务器之上,包含了 REST, HTML, 以及 WebSocket 风格交互的响应式网页应用程序的支持。

在 spring-webflux 中包含了两种独立的服务端编程模型:

  • 基于注解:使用到了@Controller 以及 Spring MVC 的其它一些注解;

  • 使用 Java 8 lambda 表达式的函数式风格的路由和处理。

有了 Spring Webflux, 你现在可以创建出 WebClient, 它是响应式且非阻塞的,可以作为 RestTemplate 的一个替代方案。

这里有一个使用 Spring 5.0 的 REST 端点的 WebClient 实现:

WebClient webClient = WebClient.create();
Mono person = webClient.get()
.uri("http://localhost:8080/movie/42")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.then(response -> response.bodyToMono(Movie.class));

尽管新的 WebFlux 模块给我么带来了激动人心的新能力,传统的 Spring MVC 在  Spring Framework 5.0 仍然得到了完整的支持。

返回顶部
顶部