What's New in Spring Framework 5.0

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

Now, nearly a year later, our long-awaited RC3 release will be released on July 18, 2017. This is the final release of the first GA release of Spring Framework 5.0 on the roadmap.

At a high level, Spring Framework 5.0 features can be broken down into:


JDK baseline updates


Core framework fixes


Core container updates


Functional programming including Kotlin

• Reactive
programming model


Testing improvements


Library support


Discontinued support for


Spring JDK Baseline Update for Framework 5.0 The


entire Spring framework 5.0 codebase runs on Java 8. So the minimum requirement for Spring Framework 5.0 environment is Java 8.

This is actually very important for the framework. And we as developers have been able to take advantage of all the new features in modern Java distributions. The framework version is also tasked with supporting the deprecated Java distribution.

The minimum requirement for the framework is now Java 8.

Spring Framework 5.0 was originally planned to be released on top of Java 9. Then, after more than 18 months of Java 9-based releases, the Spring team decided to unbind the Spring Framework 5.0 release from Java 9.

However, by the time Java 9 is released (scheduled for September 2017), Spring Framework 5.0 will be ready.

Core Framework Revisions The


core Spring Framework 5.0 has been revised to take advantage of the new features introduced by Java 8. Some of the more critical ones are as follows:

Based on the reflection enhancement of Java 8, method parameters in Spring Framework 5.0 can be accessed more efficiently.


The core Spring interface now provides optional declarations built on Java 8's default methods.


Use the @Nullable and @NotNull annotations to display nullable parameters and return values. This is enough to handle nulls at compile time instead of throwing NullPointerExceptions at runtime.


In terms of logging, Spring Framework 5.0 brings an encapsulation of the Commons Logging bridge module, which is called spring-jcl instead of the standard Commons Logging. Of course, the new version will also auto-detect Log4j 2.x, SLF4J, JUL (java.util.logging) without any additional bridging.

With the isFile indicator and the getFile method provided by the Resourse abstraction, the defensive programming approach is also promoted by the framework.

Core container update


Spring Framework 5.0 now supports candidate component indexing as an alternative to classpath scanning. This functionality has been added to the classpath scanner to simplify adding candidate component identifiers.

Application build tasks can define their own META-INF/spring.components file for the current project. At compile time, the source model is self-contained, and the JPA entities and Spring components are already marked.

Reading entities from the index instead of scanning the classpath makes no discernible difference for small projects with less than 200 classes. But it has a greater impact on large-scale projects. Loading component indexes is less expensive. Therefore, as the number of classes increases, the startup time of index reads will remain the same.

The cost of loading the component index is cheap. Therefore, when the number of classes continues to grow, the startup time of building the index can still remain constant, but for component scanning, the startup time will increase significantly.

What this means for us as developers of large Spring projects is that the application startup time will be greatly reduced. While 20 or 30 seconds might not seem like much, if you're going to do this hundreds of times a day, it can add up to you. Using the component index can help you live more efficiently every day.

You can learn more about component indexing on Spring's Jira.

The @Nullable annotation can now also be used as an indicator for optional injections. @Nullable imposes an obligation on consumers of objects that they must prepare a value that is expected to be null. Before this release, the only way to do this was through Android's Nullable, Checker Framework's Nullable, and JSR 305's Nullable.

Some other new features and enhancements in the release notes include:


Implementing functional programming style in GenericApplicationContext and AnnotationConfigApplicationContext.


Consistency checks for transactions, caching, and asynchronous annotations of interface methods.


Simplifies XML configuration namespaces into unversioned schemas.


Functional programming with Kotlin


Spring Framework 5.0 introduces support for the JetBrains Kotlin language. Kotlin is an object-oriented language that supports the functional programming style. Kotlin runs on the JVM, but the runtime environment is not limited to the JVM.

With support for Kotlin, developers can do deep functional Spring programming, especially in functional web endpoints and bean registration.

In Spring Framework 5.0, you can write clean and idiomatic Kotlin code for functional APIs on the web like this:





01
{



02
("/movie" and accept(TEXT_HTML)).nest {



03
GET("/", movieHandler::findAllView)



04
GET("/{card}", movieHandler::findOneView)



05
}



06
("/api/movie " and accept(APPLICATION_JSON)).nest {



07
GET("/", movieApiHandler::findAll)



08
GET("/{id}", movieApiHandler::findOne)



09
}



10
}

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





1
val context = GenericApplicationContext {



2
registerBean()



3
registerBean { Cinema(it.getBean()) }



4
}

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

The Reactive Programming Model One of the exciting features of


this Spring release is the new reactive stack web framework. This stack is fully reactive and non-blocking, suitable for event loop style processing, and can be scaled with a small number of threads.

Reactive Streams is an API specially developed by engineers from Netflix, Pivotal, Typesafe, Red Hat, Oracle, Twitter and Spray.io. It provides a public API for the implementation of reactive programming implementations that implement Hibernate's JPA. Here JPA is the API, and Hibernate is the implementation.

The Reactive Streams API is part of the official release of Java 9. In Java 8, you will need to specifically import dependencies to use the Reactive Streams API.

Spring Framework 5.0's support for streaming is built on Project Reactor, which specifically implements the Reactive Streams API.

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 responsive web applications running on the server, including REST, HTML, and WebSocket-style interactions.

Two separate server-side programming models are included in spring-webflux:

Annotation-based: @Controller and other Spring MVC annotations are used;


Functional-style routing and processing using Java 8 lambda expressions.


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

Here's a WebClient implementation using Spring 5.0's REST endpoints:





1
WebClient webClient = WebClient.create();



2
Mono person = webClient.get()



3
.uri("http://localhost:8080/movie/42")



4
.accept(MediaType.APPLICATION_JSON)



5
.exchange()



6
.then(response -> response.bodyToMono(Movie.class));

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

Improvements in testing


Spring Framework 5.0 fully supports JUnit 5 Jupiter, so JUnit 5 can be used to write tests and extensions. In addition to providing a programming and extension model, the Jupiter subproject provides a test engine to run Jupiter-based tests on Spring.

In addition, Spring Framework 5 provides extensions for parallel testing in the Spring TestContext Framework.

For the reactive programming model, spring-test now also introduces support for Spring WebFlux's WebTestClient integration test, similar to MockMvc, which does not require a running server. Using a mock request or response, WebTestClient can bind directly to the WebFlux server-side facility.

You can find a full list of enhancements to this exciting TestContext framework here.

Of course, Spring Framework 5.0 still supports our old friend JUnit! As I write this, JUnit 5 is only in GA. For JUnit4, Spring Framework will support for some time in the future.

Library Support


Spring Framework 5.0 currently supports the following upgraded library versions:

Jackson 2.6+


EhCache 2.10+ / 3.0 GA


Hibernate 5.0+


JDBC 4.0+


XmlUnit 2.x+


OkHttp 3.x+


Netty 4.1+


discontinued support


At the API level, Spring Framework 5.0 no longer supports the following packages:

beans.factory.access


jdbc.support.nativejdbc

• mock.staticmock for
the spring-aspects module


web.view.tiles2M. (Tiles 3 minimum required)


orm.hibernate3 and orm.hibernate4. Currently Hibernate 5 is the supported framework.


Spring Framework 5.0 also discontinued support for the following libraries:

Portlet.


Velocity.


JasperReports.


XMLBeans.


JDO.


Guava.


If you are using any of the above packages, it is recommended that you maintain Spring Framework version at 4.3. x.

Conclusion


The highlight of Spring Framework 5.0 is definitely reactive programming, which is a major paradigm shift. You can use Spring Framework 5.0 as the base version for reactive applications. For the rest of 2017 and beyond, you can expect to see subprojects implement responsive features. You'll see the reactive programming capabilities available in upcoming releases of Spring Data, Spring Security, Spring Integration, and more.


The Spring Data team has implemented reactive support for MongoDB and Redis.
It's too early to get reactive support with JDBC. The JDBC specification is inherently blocking, and it will be a while before you see reactive programming in traditional JDBC databases.
While reactive programming is the shining light in Spring Framework 5.0, it won't be supported anywhere. Downstream technologies need to provide reactive support.
As reactive programming becomes more and more popular, we can expect more and more technologies to implement reactive solutions. Of course, we can expect the Spring framework to evolve with the use of other reactive programming scenarios

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326119307&siteId=291194637