The just-released Spring Boot 3.2.0 version arrives packed with optimizations that take advantage of the latest Java platform capabilities. By embracing key Java language enhancements, Spring's programming models gain more simplicity, flexibility, and scalability.
Let's explore some of the notable new Java features Spring is now employing to enable cleaner application development.
Java Virtual Threads Boost Asynchronicity and Concurrency
The headlining Java 19 addition that Spring adopts is virtual threads. This fundamental concurrency primitive from Project Loom delivers more lightweight and efficient async execution compared to OS threads.
The Spring Framework itself along with libraries like Spring WebFlux, Spring Scheduling, and Reactor now execute asynchronously via virtual threads rather than traditional threads. For example, the WebFlux HTTP server implementation and client now utilize virtual threads for event loop execution when running on JDK 19.
By transparently mapping many application-level virtual threads onto a smaller set of OS threads, the Java Virtual Machine can drive more scalable concurrency. And virtual threads automatically pause when blocking instead of wastefully occupying a scarce system thread.
As a developer, this means we can write async/non-blocking code without callbacks using virtual threads. For example:
// Virtual Threads
CompletableFuture<ResponseEntity<String>> responseFuture = restTemplate
.getForEntity(url, String.class);
String response = responseFuture.get();
The virtual thread handling this async REST call simply pauses when awaiting the response rather than blocking an OS thread!
Records: Immutable Data Classes sans Boilerplate
Java 16 records provide a clean syntax for declaring immutable data carrier classes without all the tedious boilerplate of getters, constructors, equals, etc. Spring Data JDBC, MongoDB, Redis and other data access modules now allow mapping Java records to database tables or document collections.
For example, we can define and persist a Person record with ease:
// Records
record Person(String name, int age) {}
Person person = new Person("Jane", 22);
personRepository.save(person);
Much less code than a traditional class while still ensuring encapsulation!
Sealed Classes Manage Inheritance Extensibility
Java 17 sealed classes allow strictly controlling which supertypes a class permits for extension by inheriting subclasses. This guides flexibility while limiting inheritance to an approved list of subtypes.
Spring Security employs sealed classes to dictate the extensibility of password encoder implementations. Custom encoders can only override specific permitted subtypes like:
// Sealed Classes
public abstract sealed class DelegatingPasswordEncoder
permits BCryptPasswordEncoder, Argon2PasswordEncoder {
public String encode(CharSequence rawPassword);
public boolean matches(CharSequence rawPassword, String encodedPassword);
public abstract String getEncodingId();
}
public final class BCryptPasswordEncoder
extends DelegatingPasswordEncoder {
@Override
public String getEncodingId() {
return "bcrypt";
}
// Other methods..
}
Framework designers can thereby provide extension points without full class hierarchy exposure.
Pattern Matching Simplifies Conditional Logic
Java 14 debuted pattern matching for switch statements, which Java 17 enhanced to remove fallthrough restrictions. Spring Framework uses pattern matching in selective spots to tidy up conditional logic using switch.
For example, when detecting the reactive pull protocol type, code now simplifies from this:
if(protocol == Protocol.MQTT) { }
else if(protocol == Protocol.AMQP) { }
To the more declarative:
// Pattern Matching
switch(protocol) {
case Protocol.MQTT:
// Handle MQTT
break;
case Protocol.AMQP:
// Handle AMQP
break;
}
Many other Java innovations like text blocks, helpful null analysis, DateTime classes, and more also have sprinklings of adoption throughout Spring.
By continually embracing Java platform advancements, Spring Framework progresses with first-class support for modern application building with object-oriented design and reactive architectures. The future looks bright as the Java language charges forward!