BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Spring Framework 5.0 Released

Spring Framework 5.0 Released

Leia em Português

This item in japanese

Spring Framework 5.0, featuring a new reactive web framework, was released on September 28, 2017. Pieter Humphrey, product marketing manager at Pivotal, called it the most significant release since the project’s inception in 2004.

With this release, Spring 5.0 has its entire codebase based on Java 8 source code level. Spring 5.0 is compatible with JDK 9 for development and deployment.

Spring 5.0’s reactive web framework Spring WebFlux is built on Project Reactor 3.1 and supports both annotated and functional styles of programming. InfoQ spoke to Rossen Stoyanchev, senior staff engineer at Pivotal, about his thoughts on Spring 5.0’s emphasis on reactive programming.

Stoyanchev: Java developers are familiar with the benefits of a continuation style API for composing asynchronous logic from CompletableFuture in Java 8. In recent years we’ve also seen the rise of libraries for composing asynchronous logic, such as RxJava and Reactor, with similar benefits as CompletableFuture, but for a stream of values and with support for backpressure (as modelled by the Reactive Streams specification).  

Spring Framework 5 enables applications to embrace this asynchronous programming model with a web framework that is itself fully asynchronous in its own core contracts, uses non-blocking I/O internally, and supports Reactive Streams backpressure on top of async runtimes such as Netty and Undertow and also Servlet containers such as Tomcat and Jetty (based on Servlet 3.1 non-blocking I/O). The benefit of this approach is first-class support for composing asynchronous logic coupled with an event-loop style execution model that can handle more concurrency with less hardware resources, in particular under high load.

Stoyanchev said that the changes in Spring 5.0 will not have any immediate impact on existing applications looking to upgrade since the new functionality exists side by side with Spring MVC.

In most cases applications will import either spring-webmvc (Servlet stack) or spring-webflux (Reactive stack), and existing applications can easily be upgraded to Spring Framework 5’s version of spring-webmvc.

According to Stoyanchev, Spring 5.0 aims to provide a choice and consistency across Spring MVC and Spring WebFlux as much as possible. Both web frameworks support the same annotation-based programming model with flexible controller method signatures. In addition to this, Spring WebFlux also offers an alternative, functional web endpoint programming model that can be appealing with Java 8 lambdas and built-in Kotlin extensions.

In Spring Framework 5 we’ve actually extended Spring MVC with support for reactive return values which allows a Spring MVC controller to take advantage of the reactive WebClient and other reactive libraries such as reactive data repositories, while still operating within a Servlet-based web endpoint arrangement on any Servlet 3.1+ container.   

InfoQ asked Stoyanchev about the precautions a developer should take while adopting the reactive programming model with Spring 5.0

Developers should know that the shift from writing imperative style logic to using declarative, asynchronous APIs has a big learning curve. Asynchronous and non-blocking code is also harder to debug because you no longer have a single call stack. If you’re new to all this, start small and allow plenty of time to learn and adapt. Pick carefully which applications are a good fit and might benefit the most from non-blocking concurrency. Measure and prove the performance benefits before you decide to proceed. Remember there are simple places to start such as using the reactive WebClient in an existing Spring MVC application.

InfoQ also spoke with Juergen Hoeller, Spring Framework project lead, about his thoughts on Spring 5.0.

Hoeller: Our Java 8+ baseline brings significant API refinements and many internal optimizations across the framework. At the same time, Spring Framework 5.0 ships with comprehensive JDK 9 support on the classpath as well as the module path already.

The core container comes with functional bean registration mechanisms for Java 8 and Kotlin.

Hoeller added that Spring 5.0 also provide Kotlin extensions for common Spring API’s such as JdbcTemplate and RestTemplate, and support for data binding against Kotlin data classes.

Hoeller summarized the other noteworthy features of Spring 5.0

Spring Framework 5.0 supports the Java EE 8 API level at runtime: e.g. Servlet 4.0 for PushBuilder injection into Spring MVC handler methods, the standard JSON Binding API for JSON conversion (as an alternative to Jackson and Gson), JPA 2.2 persistence, and Bean Validation 2.0 for annotation-driven validation. All of that said, our baseline remains at Java EE 7+ level, still supporting any Servlet 3.1 container, JPA 2.1 provider, etc.

Rate this Article

Adoption
Style

Educational Content

BT