Table of Contents
General overview
This article is based on Spring Native 0.10.6 BETA documentation.
? but version 0.11 has just been released and it’s highly recommendable to use v0.11 for many improvements with a new AheadOfTime (AOT) engine. Check the Version 0.11 section in this article.
So let’s take a look at the benefits and limitations of this first version. I recommend checking out the full overview section in the official documentation.
Always keep in mind that we are working with a BETA version that has some limitations & incompatibilities. In the official documentation we can find a troubleshooting guide for the most common problems. Also have a look at the examples & issues in the github project.
Pre-requisites
- 16GB RAM (in the pc compiling to native)
- Spring Boot 2.5.6
- Java 8, Java 11, and Kotlin 1.5+
- GraalVM version 21.2.0
Spring-boot compatibility
- Spring-boot starters: Check out all supported ones, some need special dependency management.
- Spring Cloud 2020.0.3 starters
- Spring data & others (Lombok, jdbc drivers, gprc, spring-kafka, …)
Limitations
https://www.graalvm.org/reference-manual/native-image/Limitations/
Spring Native with BuildPacks or Native Build Tools
In this article we’ll follow the, both, Getting Started Guides with Buildpacks, and with Native Build Tools in the Spring Native Documentation.
You can clone the project for this article build it if you like.
git clone https://github.com/davidgfolchApium/spring-native-beta.git
cd spring-native-beta
It’s the same rest-api example used in the Spring Native documentation but with all modifications needed for both builds.
With Buildpacks
It allows us to build & run with a single command line
mvn spring-boot:build-image
It worked out-of-the-box, as usual in the Spring ecosystem.
It took 30 minutes to finish that process the first time.
You can go for a break…
Note that next builds should go kicker because we already have some buildpacks/docker things done.
After that, run it:
docker run --rm -p 8080:8080 rest-service-complete:0.0.1-SNAPSHOT
And check it works: http://localhost:8080/greeting should return
{"id":1,"content":"Hello, World!"}
With Native Build Tools
We will use the same project but installing graalvm directly. You’ll need sdkman (recommended option) to install graalvm, and the native-extensions to the jdk.
sdk install java 21.2.0.r8-grl
sdk use java 21.2.0.r8-grl
gu install native-image
Package native app:
mvn -Pnative -DskipTests package
Run it:
./target/spring-native-beta
And check it works: http://localhost:8080/greeting should return
{"id":1,"content":"Hello, World!"}
Spring Ahead of Time
We can configure some Spring AOT options to reduce foot-print, some are already disabled/enabled by default.
Native hints
We must use native hints to workaround some problems with:
- Libraries not supported by Spring Native
- Define any reflection needed by your app or any library (as Jackson)
Spring native AOT plugin will generate all *.properties & *.json configuration files needed in META-INF/native-image.
This specific configuration for our application could be set as properties files or with annotations (see spring-native-configuration project).
Samples repository
In this source repository examples we can check “the state of the art”, of several aspects, any Spring native application could need (build tools, agents, libraries, spring modules, etc).
The Pet Clinic web app integrates some of them.
Native image options
Some GraalVM options are defaulted by Spring, and some are called “Useful options”. And among these ones I will remark the –enable-https we need to specify if we have to serve through this protocol.
Tracing agent
It seems to be a “great” tool that will help generate all Native Hints needed for our new applications or libraries we could use.
Version 0.11
In this new version, the AOT engine has been changed, achieving:
- better startup & memory improvements, compatibility improvements, but less runtime flexibility.
- a new architecture plugin called “Extension points” that allows to programmatically configure AOT hints.
- New “AOT testing support” with Junit5 and graalvm native-build-tools (mockito not supported yet).
- Others…
Spring is already planning “Spring Boot 3 native support” that will integrate this new version into the standards.
Author
-
15 years as a technical/functional analyst and java backend engineer. 8 years: backend/full-stack SOLID architecture designs and POC's implementations… leading & mentoring dev teams in best practices
View all posts