#REVIEW: What’s new in Spring Boot 2.1

Recently Andy Wilkinson (@ankinson) from the Spring Boot core team announced the release of Spring Boot 2.1. During the last days, I played around with the new version and will present you the most important updates from my point of view.

Support for Java 11

I think the most important update for this new release is the support of Java 11 which was released in September this year and is the latest LTS version of Java. Trying the new features of Java 9, 10 and 11 with just plain Java SE is nice, but without the support of your framework, you can’t utilize the features in production. To my knowledge Spring is also the first major Java framework which supports Java 11 and was able to relase a production-ready version about one months after Java 11 GA. Bootstrapping a new Spring Boot project with Java 11 will result in the following pom.xml and will out of the box with no additional configuration.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>de.rieckpil.blog</groupId>
  <artifactId>whats-new-in-spring-boot-2.1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>whats-new-in-spring-boot-2.1</name>
  <description>Updates to Spring Boot 2.1</description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>11</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

Third-party library upgrades

Spring Boot 2.1 upgrades to the core Spring Framework with version 5.1 which comes with initial refinements for GraalVM, functional bean definitions for Java and Kotlin, improved startup time and less heap memory consumption due to optimizing the internal use of reflection.

Other notable upgrades are the following:

Other news

Logging Groups

We are now able to group different packages for which we want to define a common log level in a so-called logging group. With this you can group e.g. all your business logic related classes into a group and define DEBUG as the log level and TRACE for example for your messaging interfaces which will log all the incoming messages:

# define the group
logging.group.corelogic=de.rieckpil.blog.payments, de.rieckpil.blog.transfers ...

# use the group (possibly in a different configuration file)
logging.level.corelogic=DEBUG

There are also to pre-defined logging groups web and sql which are provided out-of-the-box.

Bootstrap mode for JPA setup

Bootstrapping a Spring Boot application with a lot of JPA entities and JPA repositories from Spring Data can influence the startup time of your application. With the Spring Data Lovelace release, they introduced a so-called bootstrap mode where you can define the initialization of the repositories. In the default mode, the full initialization happens during the startup of the application. The deferred mode uses a background JPA infrastructure initialization deferred until the ApplicationContext  has completed its initialization. With lazy you get the JPA infrastructure initialization deferred until the first access:

spring.data.jpa.repositories.bootstrap-mode=lazy

For a sample project to show the differences, have a look at the following repository: Spring Data JPA – Deferred bootstrap modes

JUnit 5 improvements

With Spring Boot 2.1 you can remove the required @ExtendWith(SpringExtension.class) annotation if you are using a Spring Boot testing annotation @…​Test like @DataJpaTest as they are all now meta-annotated with the Spring extension. In addition, you now don’t have to do any custom setup for your Maven plugins to make use of JUnit 5.

@SpringBootTest
// no @ExtendWith(SpringExtension.class) needed
public class ApplicationTests {

  @Test
  @DisplayName("Load the whole Spring context")
  public void contextLoads() {
  }

}

There are a lot more upgrades and news but these were one of the most useful ones for my projects. You find the full list of news in the official Release Notes on GitHub.

A sample Spring Boot 2.1 project with Java 11 can be found in my GitHub repository.

See you,

Phil

1 Comment

  1. Pingback: Java Weekly, Issue 254 | Baeldung

Leave a comment

Your email address will not be published. Required fields are marked *