Spring Framework 5.0 新特性 已翻译 100%

达尔文 投递于 2017/07/05 12:44 (共 15 段, 翻译完成于 07-05)
阅读 13228
收藏 62
5
加载中

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.

已有 2 人翻译此段
我来翻译

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.

已有 1 人翻译此段
我来翻译

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.

已有 1 人翻译此段
我来翻译

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.

已有 1 人翻译此段
我来翻译

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. 

已有 1 人翻译此段
我来翻译

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.

已有 1 人翻译此段
我来翻译

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.

已有 1 人翻译此段
我来翻译

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!

已有 1 人翻译此段
我来翻译

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.

已有 1 人翻译此段
我来翻译

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.

已有 1 人翻译此段
我来翻译
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接。
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
加载中

评论(25)

xuezhongyu01
xuezhongyu01
webflux是不是不支持mysql数据库?
谁来与我大战三百回合
滥用函数式,就是丑陋+代码不易维护。
小杨阿哥哥
小杨阿哥哥
Velocity 被下架了!!
公孙二狗
公孙二狗

引用来自“antipro”的评论

就因为velocity停止更新,就放弃支持了,这是什么道理啊?

引用来自“南湖船老大”的评论

velocity确实老迈不堪了,有太多可替代者,比如beetl,比如theymleaf
//在模板里
<%
for(user in list){
%>
hello,${user.name};
<% } %>

最讨厌 JSP 这种格式的模版 <% %>
东东-
东东-
早看过了, 你们的 kotlin dsl 设计的好垃圾, 还不如用老式的注解算了.
剑无锋o
剑无锋o
什么时候正式版发布
南湖船老大
南湖船老大

引用来自“阿信sxq”的评论

文档里面早就描述了当初选择commons-logging是一个错误,为什么过了这么久,这次还专门写桥接器,为什么就不能切换呢,难道难度真的有那么大?
这确实做得不对
南湖船老大
南湖船老大

引用来自“antipro”的评论

就因为velocity停止更新,就放弃支持了,这是什么道理啊?
velocity确实老迈不堪了,有太多可替代者,比如beetl,比如theymleaf
antipro
antipro
就因为velocity停止更新,就放弃支持了,这是什么道理啊?
南宫紫英
南宫紫英

引用来自“notreami”的评论

kotlin语法好丑的样子。

引用来自“orpherus”的评论

已经把项目中100多个文件替换成了kotlin,接盘侠肯定会很喜欢
亮了
返回顶部
顶部