Getting Ready for Spring Framework 6

Getting Ready for Spring Framework 6

0 Comments

On December 16th, 2021, with the announcement from the Spring Team, the next major release is Spring Framework 6. The whole objective is to cover Spring’s baseline upgrade efforts, in particular requiring JDK 17+ and migrating to the Jakarta EE 9 APIs, So we should be Getting Ready for upgrading to Spring Framework 6.

Important Note from the Official Doc of Java 17: “These older versions of the JDK are provided to help developers debug issues in older systems. They are not updated with the latest security patches and are not recommended for use in production.”

So what does this means for us as Spring Developers?

In this post, I will walk through the key feature of this major version upgrade of Spring.

Setting up the Environment

Spring Framework 6 brings in several new features. I will pick up the key ones that you developers should know and can put to use. As the new framework targets Java 17, let’s start playing with Spring 6 with Java 17.

If you are hesitant on changing the Java version because of your organizational requirements or your personal attachment, I suggest you go through the following two posts of mine:

Using SDKMAN for Your Development Environment
Using SDKMAN To Manage Java Versions

For non-window users after exporting JAVA_HOME and PATH, your echo $JAVA_HOME should appear similar to this.

Setting JAVA_HOME

For Windows users: Use echo %JAVA_HOME%

As now we are in Java 17, let’s explore Spring 6.

The Upgrade Process

With this upgrade of the Spring Framework accompanied by Spring Boot upgrade to v3 will require a minimum of JDK 17 at runtime.

Also, a minimum of Tomcat 10 / Jetty 11 (for Jakarta EE 9 compatibility).

There may be some changes required in your application source code.

For example, you will need to refactor the javax to jakarta namespace change wherever you’re using the Servlet API, JPA, Bean Validation, etc.

This makes sense, as, by Q4 2022, we will have JDK 17 superseding JDK 11 as the next Long-Term Support (LTS).

However, the good news on the official doc is:

“If you intend to stay on JDK 8 or 11 for a further few years, or even if you intend to stay on the Spring Framework 5.3 infrastructure on JDK 17 for several years, rest assured that our extended maintenance of Spring Framework 5.3.x and Spring Boot 2.x will keep you covered.”

Note: I don’t consider enforcing Java 17 for Spring 6 is as aggressive as it sounds today; by the time Spring 6 ships, Java 19 will have been released.

You should refer Upgrading to Spring Framework 6.0 page which the Spring Team will keep updating. Follow the main branch on GitHub for the latest changes, as 6.0 M2 is getting prepared.

Also, the corresponding Spring Boot 3.0 M1 release is planned for January. At that point, you will also be able to consume Spring Framework 6.0 through Spring Starter.

For the time being, feel free to grab 6.0 M1 from https://repo.spring.io/.

This is where you can search and find the latest builds of the various Spring modules,

Spring Repo

What is New in Spring Framework 6

To try out the new Spring Framework Features, let’s head to the Spring Starter site.

Do you Know? Spring Starter generates about 1.5 million Spring Boot downloads. This is huge and demonstrates how developers are moving to Spring to build modern applications – particularly Spring Microservices.

If you want to dive into Spring Microservices, check out my Udemy Best Seller course Spring Boot Microservices with Spring Cloud Beginner to Guru

Ok!  So what I did is to create a bare minimal Spring Boot 3 (M1) project in Spring Starter that is backed with Spring Framework 6.

Note that I have selected Java 17.

To keep this simple, I haven’t added any dependencies.

Creating a New Spring 6 Project in Spring Starter

 

On clicking Generate, you will get the project structure downloaded that you can open in IntellIJ and examine.
I first opened the pom.xml file and here is what I have:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.0-M1</version>
    <relativePath/> <! – lookup parent from repository – >
  </parent>
  <groupId>guru.springframework</groupId>
  <artifactId>spring-6-spring-bppt-5-pdemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring-6-spring-bppt-5-pdemo</name>
  <description>Demo project for Spring Boot</description>
  <properties>
    <java.version>17</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</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>
  <repositories>
    <repository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </pluginRepository>
  </pluginRepositories>

</project>

 

Starter Dependencies

The first exciting thing in this new release is Spring Boot Starter dependencies. Earlier in Spring Starter we declare our starter dependencies. Based on it, Maven transitively pulls down the other related dependencies.

But what happens, if you later want another dependency? Now it has become easy because of changes brought in by the new version in the Spring Starter infrastructure.

Spring Observability

Spring 6 introduces Spring Observability – a new project that builds on Spring Cloud Sleuth. It is more for efficiently recording application metrics with Micrometer and implementing tracing through providers such as OpenZipkin or OpenTelemetry.

Spring Observability scores over previous agent-based observability, as it works seamlessly in natively compiled Spring applications to more effectively provide better information.

Java Platform Module System

Way back, Java 9 introduced a new level of abstraction above packages. It was formally known as the Java Platform Module System (JPMS), but there wasn’t observable adoption. The new Spring Framework 6 embraces it.
Concourse Pipeline.

From a long time back, one continuous demand from Spring developers was to have a dedicated build pipeline. We used external pipelines, starting from Github. Gitlab CI/CD pipelines, Jenkins, CircleCI Travis, and so on.

Finally, the new Spring Framework 6 brings to us Concourse for its CI build and other automated tasks. So while you move towards upgrading to Spring Framework 6, make use of the dedicated Concourse instance available at https://ci.spring.io with a build pipeline for Spring Framework 6.0.x.

What do we Developers need to know?

Changes in Core Container

The @Injectannotation belongs to the JSR-330 annotations collection.

In Spring Framework 6, it is to be found in jakarta.inject.

Similarly, JSR-250 based annotations, such as, @PostConstruct and
@PreDestroy are now to be found in jakarta.annotation.

Note: For the time being, Spring keeps detecting their javax equivalents as well, covering common use in pre-compiled binaries.

Change of HttpMethod from Enum to Class

HTTP methods as per the HTTP specification, s not limited to the well-known set (GET, HEAD, PUT, POST, and so on. But, they can also be an “extension-method”.

Well-known extensions include WebDAV, which added methods like LOCK, COPY, and MOVE.

Prior to Spring Framework 6, HTTP methods were part of the HttpMethod enum.

As a result, the Spring Framework needs several workarounds. In order to allow for HTTP methods not in the enum, such as having both
HttpRequest::getMethod as well as HttpRequest::getMethodValue.

Spring Framework 6 changed HttpMethod from
enum to
class.

It is obvious we will need a new build, but this was long due.

Data Access and Transactions

Because of the new Spring Framework 6 migrating towards Jakarta EE, we need to ensure upgrading to:

Hibernate ORM 5.6.x with the
hibernate-core-jakarta artefact. What this means is if we are directly coding against Hibernate we need to switch from javax.persistence imports to
jakarta.persistence.

Also, we need to have the corresponding Hibernate Validator generation dependency version 7.0.x, which is based on
jakarta.validation.

Web Applications

With the new Spring Framework 6 release, ensure to upgrade to Tomcat 10, Jetty 11, or Undertow 2.2.14 with the
undertow-servlet-jakarta.

Also, ensure you upgrade to jakarta.servlet imports from javax.servlet.

Spring Framework 6 has dropped several outdated Servlet-based integrations. A few examples are Commons FileUpload, Tiles, FreeMarker JSP support.

Instead, Spring Framework 6 recommends StandardServletMultipartResolver for multipart file uploads and regular FreeMarker template views if needed.

This new version focuses more on REST-oriented web architectures.

Also, the new Spring Framework 6 version won’t detect Spring MVC and Spring WebFlux on a type-level
@RequestMapping annotation. The consequence is that interfaced-based AOP proxying for web controllers may no longer work. As developers, ensure you enable class based proxying for such controllers. Alternatively mark your interface with
@Controller.

Summary

Spring Framework 6 has just been released and the developer community is yet to respond. I just scratched the surface in this post.

In the next post, I will attempt to upgrade the pet-clinic application of the Udemy Best Seller Course Spring Framework 5: Beginner to Guru for upgrading to Spring Framework 6 and share my experience.


About SFG Contributor

Staff writer account for Spring Framework Guru

    You May Also Like

    Leave a Reply

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

    This site uses Akismet to reduce spam. Learn how your comment data is processed.