What is New in Spring Boot 2.2?
0 CommentsLast Updated on November 27, 2019 by jt
As of writing this post, Spring Boot 2.2 was released on October 16th, 2019. This new release is built on Spring Framework 5.2.
The Spring Boot 2.2 has a number of great features, which we’ll explore in this post.
Spring Framework and Java
Spring Framework 5.2
Spring Boot 2.2 uses Spring Framework 5.2. A crucial enhancement in Spring Framework 5.2 is a more consistent and efficient annotation retrieval algorithm.
Prior to Spring Framework 5.2, StandardAnnotationMetadata
operates against Class.getAnnotations()
. This potentially leads to seeing inherited annotations as well. However, inherited annotations aren’t relevant for component and configuration class introspection purposes.
Also, this is inconsistent within the AnnotationMetadata
abstraction. Because ASM implementation operates against locally declared annotations only.
In Spring Framework 5.2, inherited annotations are consistently ignored across class-declared and component-scanned beans.
You can find other enhancements in Spring Framework 5.2 here.
Java 13 Support
Like Spring Framework 5.2, Spring Boot 2.2 supports Java 13.
Java 11 and Java 8 are also supported.
Configuration Properties
Immutable Configuration Properties Binding
A very notable feature is immutable configuration properties via constructor based binding. This is done with a new annotation @ConstructorBinding
.
The Spring Boot documentation has some great documentation on this feature.
@ConfigurationProperties Scanning
As an alternative to using @EnableConfigurationProperties
or @Component
, classes annotated with @ConfigurationProperties
can now be found via classpath scanning.
Scanning can be enabled manually.
The scanned packages can be customized, using @ConfigurationPropertiesScan
.
@SpringBootApplication @ConfigurationPropertiesScan({ "com.example.app", "org.acme.another" }) public class MyApplication { }
Testing Changes
JUnit 5
Spring Boot now ships with JUnit 5. Finally.
The JUnit 5 Vintage Engine is still included, so your JUnit 4 tests will continue to function.
JUnit 5 has a lot of great features. If you need to get up to speed on JUnit 5, check out my course – Testing Spring Boot: Beginner to Guru.
This course was completely developed with Java 11 and JUnit 5.
Mockito 3
Spring Boot 2.2 updates Mockito to version to 3.1.0.
There are no breaking API changes in Mockito 3.x. Mockito 3 does require Java 8 or higher.
Mockito 3 also has a variety of fixes to support JUnit 5.
Enhancement to AssertJ
Spring Boot 2.2 upgrades to AssertJ 3.12. You can now use the new fluent recursive comparison API that AssertJ Core provides for Object assertions. This new API covers what isEqualToComparingFieldByFieldRecursively
used to do, but easier to use and with more capabilities.
Additional Changes in Spring Boot 2.2
JMX
In Spring Boot 2.2, JMX is disabled by default. This makes sense as JMX doesn’t seem to be widely used. In addition, JMX takes a significant amount of resources affecting Spring Boot startup performance.
You can enable JMX at any time through the spring.jmx.enabled
property.
Migration to Jakarta EE
Spring Boot 2.2 replaces all Java EE dependencies with the equivalent Jakarta EE dependencies.
This is done for a quick evolution into cloud, containers, microservices, serverless, and reactive technologies.
So in Spring Boot 2.2 starters, Java EE dependencies with a javax.* group ID has changed to the equivalent Jakarta EE dependencies with a jakarta.* group ID.
Also as a part of the migration, artifact ID of the following dependencies has got updated.
com.sun.mail:javax.mail
is nowcom.sun.mail:jakarta.mail
org.glassfish:javax.el
is noworg.glassfish:jakarta.el
Changes to Hateos
Spring Boot 2.2 uses Hateos 1.0 that comes with some breaking changes. Hateos was never meant to create resources. Hateos was meant to create vendor-neutral representations of hypermedia. Hateos 1.0 aligns with this fundamental fact by renaming several of its core types:
ResourceSupport
is nowRepresentationModel
Resource
is nowEntityModel
Resources
is nowCollectionModel
PagedResources
is nowPagedModel
The earlier Hateos APIs are centered on the concept of List including RepresentationModel.getLinks()
.
Now, Instead of a List, Hateos returns Links. Introducing Links make it easier to combine, extract, and merge links.
Currently the core abstractions LinkBuilder
, EntityLinks
, RelProvider
, and LinkDiscoverer
have been grouped into server and client packages respectively.
Although the new changes appear overwhelming, you can use this migration script to ease the migration of your old code to the new types and import statements.
Another important Hateos feature available in Spring Boot 2.2 is support for Spring WebFlux and Reactive programming.
This includes:
- Building links reactively with
WebFluxLinkBuilder
. - Serving hypermedia to WebFlux endpoints.
- Support for WebFlux’s
WebClient
to consume hypermedia.
Lazy Initialization
In Spring Boot 2.2, you can enable global lazy initialization of beans by setting the spring.main.lazy-initialization
property.
When set to true
, bean definitions in the application will be configured to use lazy initialization.
Lazy initialization has these benefits:
- Reduced startup time as fewer beans are created and loaded during application start-up.
- Reduced hot restarts time of Spring Development Tools. This leads to developer productivity.
- Faster integration testing. Because enabling lazy initialization will limit the beans that are initialized to those that are needed by the test.
Lazy initialization also comes at a cost:
- Problems that would have been identified at startup can get masked due to lazy initialization
- Latency might increase for HTTP requests that trigger bean initialization
Note: You can force a bean to be initialized eagerly. You need to annotate its definition with @Lazy(false)
.
Gradle
The minimum requirements for Gradle have changed – Spring Boot now requires Gradle 4.10+.
Actuator HTTP Trace and Auditing
The Actuator HTTP Tracing and Auditing features are disabled by default. This has been done as the default repository implementations are in-memory and might consume too many resources. In addition, they are not cluster friendly.
To turn those features back on, you need a bean implementing HttpTraceRepository
or AuditEventRepository
.
Even in the presence of beans, the management.auditevents.enabled
and management.trace.http.enabled
configuration properties are available to turn those features off.
Performance Improvements
Spring Boot 2.2 comes with several performance improvements that include:
- Time taken to bind large numbers of configuration properties has been significantly reduced
- As Spring Boot fully prepares a
PersistenceUnit
by scanning JPA entities, Hibernate’s own entity scanning has been disabled as it is redundant - Injection points in auto-configurations have been refined to only apply when a bean has to be created
- Beans related to Actuator endpoints are now only created if the endpoint is both enabled and exposed (via JMX or HTTP)
Configuration Changes
There have been several configuration properties change between Spring Boot 2.1.3.RELEASE and 2.2.X. The key properties that have been deprecated are:
logging.file
has been replaced bylogging.file.name
logging.path
has been replaced bylogging.file.path
Some new properties introduced in Spring Boot 2.2 are:
logging.file.clean-history-on-start
: Whether to clean the archive log files on startup. Default value is falselogging.file.total-size-cap
: Total size of log backups to be kept. Default is 0B.server.tomcat.accesslog.max-days
: Number of days to retain the access log files before they are removed. Default is -1server.tomcat.processor-cache
: Maximum number of idle processors that will be retained in the cache and reused with a subsequent request. Default is 200spring.data.neo4j.use-native-types
: Whether to use Neo4j native types wherever possible. Default is
falsespring.datasource.dbcp2.auto-commit-on-return
spring.main.lazy-initialization
: Whether initialization should be performed lazily. Default is false.spring.security.oauth2.resourceserver.jwt.jws-algorithm
: JSON Web Algorithm used for verifying the digital signatures. Default is RS256.spring.task.execution.shutdown.await-termination
: Whether the executor should wait for scheduled tasks to complete on shutdown. Default is false.spring.task.execution.shutdown.await-termination-period
: Maximum time the executor should wait for remaining tasks to complete.spring.task.scheduling.shutdown.await-termination
: Whether the executor should wait for scheduled tasks to complete on shutdown. Default is false.spring.task.scheduling.shutdown.await-termination-period
: Maximum time the executor should wait for remaining tasks to complete.
Properties that have been removed from Spring Boot 2.2 are:
management.endpoints.jmx.unique-names
server.tomcat.max-http-header-size
spring.data.cassandra.load-balancing-policy
spring.data.cassandra.reconnection-policy
spring.data.cassandra.retry-policy
spring.kafka.streams.cache-max-bytes-buffering
spring.rabbitmq.template.queue
Miscellaneous
In addition to the preceding features, here are some additional features.
ConditionalOnCloudPlatform
now detects if the application is running on Kubernetes.loadOnStartup
can be configured if you’re wrapping a Servlet as an actuator endpoint.- Flyway bootstrap failures are covered by a dedicated
FailureAnalyzer
- The
MessageConverter
for Kafka batch listeners is properly auto-configured. - Add support for Jaybird 4 (jdbc:firebird prefix).
- Add opt-in support for
Neo4j-OGM
native types. - Client-side HTTP-metrics have an outcome tag.
- Numerous upgrades of third-party dependencies
Checkout the official announcement of Spring Boot 2.2 here.