Libro 03. Packtpub - Mastering Spring 5.0 (2017) PDF-41-47
Libro 03. Packtpub - Mastering Spring 5.0 (2017) PDF-41-47
One of the biggest features of Spring Framework 5.0 is Reactive Programming. Core
reactive programming features and support for reactive endpoints are available out of the
box with Spring Framework 5.0. The list of important changes includes the following:
Baseline upgrades
JDK 9 runtime compatibility
Usage of JDK 8 features in the Spring Framework code
Reactive programming support
A functional web framework
Java modularity with Jigsaw
Kotlin support
Dropped features
Baseline upgrades
Spring Framework 5.0 has JDK 8 and Java EE 7 baseline. Basically, it means that previous
JDK and Java EE versions are not supported anymore.
Some of the important baseline Java EE 7 specifications for Spring Framework 5.0 are listed
as follows:
Servlet 3.1
JMS 2.0
JPA 2.1
JAX-RS 2.0
Bean Validation 1.1
[ 19 ]
Evolution to Spring Framework 5.0
There are many changes to the minimum supported versions of several Java frameworks.
The following list contains some of the minimum supported versions of prominent
frameworks:
Hibernate 5
Jackson 2.6
EhCache 2.10
JUnit 5
Tiles 3
Tomcat 8.5+
Jetty 9.4+
WildFly 10+
Netty 4.1+ (for web reactive programming with Spring Web Flux)
Undertow 1.4+ (for web reactive programming with Spring Web Flux)
[ 20 ]
Evolution to Spring Framework 5.0
With Spring Framework 5.0, the baseline version is Java 8. Spring Framework code is now
upgraded to use the new features in Java 8. This will result in more readable and
performant framework code. Some of the Java 8 features used are as follows:
While Java 8 does not have built-in suppport for reactive programming, there are a number
of frameworks that provide support for reactive programming:
We will discuss Reactive Programming and how you can implement it with Spring Web
Flux in Chapter 11, Reactive Programming.
[ 21 ]
Evolution to Spring Framework 5.0
A functional web framework can also be used to define more complex routes, as shown in
the following example:
RouterFunction<?> route = route(GET("/todos/{id}"),
request -> {
Mono<Todo> todo = Mono.justOrEmpty(request.pathVariable("id"))
.map(Integer::valueOf)
.then(repository::getTodo);
return Response.ok().body(fromPublisher(todo, Todo.class));
})
.and(route(GET("/todos"),
request -> {
Flux<Todo> people = repository.allTodos();
return Response.ok().body(fromPublisher(people, Todo.class));
}))
.and(route(POST("/todos"),
request -> {
Mono<Todo> todo = request.body(toMono(Todo.class));
return Response.ok().build(repository.saveTodo(todo));
}));
We will discuss Mono and Flux in more detail in Chapter 11, Reactive Programming.
Platform Bloat: Java modularity has not been a cause of concern in the last couple
of decades. However, with Internet of Things (IOT) and new lightweight
platforms such as Node.js, there is an urgent need to address the bloat of the Java
platform. (Initial versions of JDK were less than 10 MB in size. Recent versions of
JDK need more than 200 MB.)
[ 22 ]
Evolution to Spring Framework 5.0
JAR Hell: Another important concern is the problem of JAR Hell. When Java
ClassLoader finds a class, it will not see whether there are other definitions for
the class available. It immediately loads the first class that is found. If two
different parts of the application need the same class from different jars, there is
no way for them to specify the jar from which the class has to be loaded.
Open System Gateway initiative (OSGi) is one of the initiatives, started way back in 1999,
to bring modularity into Java applications.
Each module can have its own life cycle. It can be installed, started, and stopped on its own.
Jigsaw is an initiative under Java Community Process (JCP), started with Java 7, to bring
modularity into Java. It has two main aims:
Jigsaw is expected to be part of Java 9 and Spring Framework 5.0 is expected to include
basic support for Jigsaw modules.
Kotlin support
Kotlin is a statically typed JVM language that enables code that is expressive, short, and
readable. Spring framework 5.0 has good support for Kotlin.
[ 23 ]
Evolution to Spring Framework 5.0
In fewer than 10 lines of code, we created and tested a data bean with three properties and
the following functions:
equals()
hashCode()
toString()
copy()
Kotlin is strongly typed. But there is no need to specify the type of each variable explicitly:
val arrayList = arrayListOf("Item1", "Item2", "Item3")
// Type is ArrayList
Named arguments allow you to specify the names of arguments when calling methods,
resulting in more readable code:
var todo = Todo(description = "Learn Spring Boot",
name = "Jack", targetDate = Date())
Kotlin makes functional programming simpler by providing default variables (it) and
methods such as take, drop, and so on:
var first3TodosOfJack = students.filter { it.name == "Jack"
}.take(3)
With all its features making the code concise and expressive, we expect Kotlin to be a
language to be learned for the .
We will discuss more about Kotlin in Chapter 13, Working with Kotlin in Spring.
[ 24 ]
Evolution to Spring Framework 5.0
Dropped features
Spring Framework 5 is a major Spring release with substantial increase in the baselines.
Along with the increase in baseline versions for Java, Java EE and a few other frameworks,
Spring Framework 5 removed support for a few frameworks:
Portlet
Velocity
JasperReports
XMLBeans
JDO
Guava
If you are using any of the preceding frameworks, it is recommended that you plan a
migration and stay with Spring Framework 4.3--which has support until 2019.
Jetty 9.4
Tomcat 8.5
Hibernate 5.2
Gradle 3.4
We will discuss Spring Boot extensively in Chapter 5, Building Microservices with Spring Boot
and Chapter 7, Advanced Spring Boot Features.
[ 25 ]