Ready for 2018 and beyond

Spring Framework 5.0 is here

Gabriela Motroc
Spring Framework 5.0

© Shutterstock / Zamurovic Photography

Spring Framework 5.0 is here and it’s ready for the long haul! There’s support for JDK 9 and the Java EE 8 API level, comprehensive integration with Reactor 3.1, JUnit 5, and the Kotlin language. Plus, there’s a dedicated reactive web framework called Spring WebFlux. Dig in!

Jürgen Höller, co-founder of the Spring Framework open source project and project lead and release manager for the core framework, announced the GA of Spring Framework 5.0. In short,

This brand-new generation of the framework is ready for 2018 and beyond: with support for JDK 9 and the Java EE 8 API level (e.g. Servlet 4.0), as well as comprehensive integration with Reactor 3.1, JUnit 5, and the Kotlin language. On top of that all, Spring Framework 5 comes with many functional API variants and introduces a dedicated reactive web framework called Spring WebFlux, next to a revised version of our Servlet-based web framework Spring MVC.

Spring Framework 5.0 overview

General core revision

  • JDK 8+ enhancements:
    • Efficient method parameter access based on Java 8 reflection enhancements.
    • Selective declarations of Java 8 default methods in core Spring interfaces.
    • Consistent use of JDK 7 Charset and StandardCharsets enhancements.
  • JDK 9 compatibility:
    • Avoiding JDK APIs which are deprecated in JDK 9 wherever possible.
    • Consistent instantiation via constructors (with revised exception handling).
    • Defensive use of reflection against core JDK classes.
  • Non-null API declaration at the package level:
    • Nullable arguments, fields and return values explicitly annotated with @Nullable.
    • Primarily for use with IntelliJ IDEA and Kotlin, but also Eclipse and FindBugs.
    • Some Spring APIs are not tolerating null values anymore (e.g. in StringUtils).
  • Resource abstraction provides isFile indicator for defensive getFile access.
    • Also features NIO-based readableChannel accessor in the Resource interface.
    • File system access via NIO.2 streams (no FileInput/OutputStream used anymore).
  • Spring Framework 5.0 comes with its own Commons Logging bridge out of the box:
    • spring-jcl instead of standard Commons Logging; still excludable/overridable.
    • Autodetecting Log4j 2.x, SLF4J, JUL (java.util.logging) without any extra bridges.
  • spring-core comes with ASM 6.0 (next to CGLIB 3.2.5 and Objenesis 2.6).

JDK 8+ and Java EE 7+ Baseline

  • Entire framework codebase based on Java 8 source code level now.
    • Improved readability through inferred generics, lambdas, etc.
    • Conditional support for Java 8 features now in straight code.
  • Full compatibility with JDK 9 for development and deployment.
    • On classpath as well as module path (with stable automatic module names).
    • Framework build and test suite passes on JDK 9 (runs on JDK 8 by default).
  • Java EE 7 API level required in Spring’s corresponding features now.
    • Servlet 3.1, Bean Validation 1.1, JPA 2.1, JMS 2.0
    • Recent servers: e.g. Tomcat 8.5+, Jetty 9.4+, WildFly 10+
  • Compatibility with Java EE 8 API level at runtime.
    • Servlet 4.0, Bean Validation 2.0, JPA 2.2, JSON Binding API 1.0
    • Tested against Tomcat 9.0, Hibernate Validator 6.0, Apache Johnzon 1.1

Kotlin support

This release also introduces first-class support for Kotlin. Developers can now write Spring + Kotlin applications almost as if the Spring Framework was a native Kotlin framework. Read more about it here.

If you want an overview, check out What’s new in the Spring Framework:

  • Null-safe API when using Kotlin 1.1.50+
  • Support for Kotlin immutable classes with optional parameters and default values
  • Functional bean definition Kotlin DSL.
  • Functional routing Kotlin DSL for WebFlux.
  • Leveraging Kotlin reified type parameters to avoid specifying explicitly the Class to use for serialization/deserialization in various APIs like RestTemplate or WebFlux APIs.
  • Kotlin null-safety support for @Autowired/@Inject and @RequestParam/@RequestHeader/etc annotations in order to determine if a parameter/bean is required or not.
  • Kotlin script support in ScriptTemplateView for both Spring MVC and Spring WebFlux.
  • Array-like setters added to ModelModelMap and Environement.
  • Support for Kotlin autowired constructor with optional parameters
  • Kotlin reflection is used to determine interface method parameters

Let’s not forget about the complete support for JUnit 5‘s Jupiter programming and extension models in the Spring TestContext Framework, support for parallel test execution in the Spring TestContext Framework and more. We haven’t even discussed the revised version of their Servlet-based web framework Spring MVC so make sure to read What’s new in the Spring Framework.

Spring WebFlux — a dedicated reactive web framework

Pivotal has put together a Spring Framework 5.0 FAQ. We won’t go into (too many) details but you should read it — it will (hopefully) answer all your questions about the new release. What we’re interested in is Spring WebFlux so if you’re wondering why Pivotal introduced a dedicated reactive web framework, here’s the answer:

Blocking threads consume resources. For latency sensitive workloads which need to handle a large number of concurrent requests, the non-blocking async programming model is more efficient. This is particularly relevant for mobile applications and interconnected microservices.

The goal of Spring WebFlux is to offer Spring developers a non-blocking event-loop style programming model similar to node.js. For more details and demos, watch Servlet vs Reactive Stacks in Five Use Cases and Reactive Web Applications with Spring Framework 5, both by Rossen Stoyanchev.

Spring WebFlux — What’s new

  • New spring-webflux module, an alternative to spring-webmvc built on a reactive foundation — fully asynchronous and non-blocking, intended for use in an event-loop execution model vs traditional large thread pool with thread-per-request execution model.
  • Reactive infrastructure in spring-core such as Encoder and Decoder for encoding and decoding streams of Objects; DataBuffer abstraction, e.g. for using Java ByteBuffer or Netty ByteBufReactiveAdapterRegistry for transparent support of reactive libraries in controller method signatures.
  • Reactive infrastructure in spring-web including HttpMessageReader and HttpMessageWriter that build on and delegate to Encoder and Decoder; server HttpHandler with adapters to (non-blocking) runtimes such as Servlet 3.1+ containers, Netty, and Undertow; WebFilterWebHandler and other non-blocking contract alternatives to Servlet API equivalents.
  • @Controller style, annotation-based, programming model, similar to Spring MVC, but supported in WebFlux, running on a reactive stack, e.g. capable of supporting reactive types as controller method arguments, never blocking on I/O, respecting backpressure all the way to the HTTP socket, and running on extra, non-Servlet containers such as Netty and Undertow.
  • New functional programming model (“WebFlux.fn”) as an alternative to the @Controller, annotation-based, programming model — minimal and transparent with an endpoint routing API, running on the same reactive stack and WebFlux infrastructure.
  • New WebClient with a functional and reactive API for HTTP calls, comparable to the RestTemplate but through a fluent API and also excelling in non-blocking and streaming scenarios based on WebFlux infrastructure; in 5.0 the AsyncRestTemplate is deprecated in favor of the WebClient.

Here’s how to migrate to Spring Framework 5.0.

Spring Framework 5.0 is available as 5.0.0.RELEASE from repo.spring.io and Maven Central.

Author
Gabriela Motroc
Gabriela Motroc is an online editor for JAXenter.com. Before working at S&S Media she studied International Communication Management at The Hague University of Applied Sciences.

Comments
comments powered by Disqus