How to Integrate Spring Native Into Spring Boot Microservices | by Wenqi Glantz | Apr, 2022

Spring Native: the wings that make Spring Boot fly

Picture background by finix8 from Pixabay

Regardless of its huge recognition in internet and microservices improvement, Spring Boot has its limitations. Most noticeably, lengthy startup time, and excessive reminiscence consumption. The basis trigger lies in how Spring depends on reflection to examine lessons, interfaces, fields, and strategies at runtime. Is it doable to have the identical productiveness with out using reflection at runtime?

Sure! Spring Native has the reply!

We’re going to discover on this story methods to combine Spring Native right into a Spring Boot microservice named customer-service, a easy CRUD software to handle clients. We’re going to have two customer-service apps, one vanilla Spring Boot app named customer-service, the opposite, customer-service-native, a Spring Boot app with Spring Native baked in. We’re going to take an in depth take a look at the efficiency increase Spring Native brings to Spring Boot.

A sneak peak under on the startup time comparability between customer-service and customer-service-native.

Blazing quick startup for the native Spring Boot app! To learn the way this may be carried out, learn on.

Spring Native compiles Spring functions to native executables utilizing the GraalVM native picture compiler at construct time. In comparison with JVM, GraalVM native pictures can allow cheaper and extra sustainable internet hosting for a lot of forms of workloads. Utilizing GraalVM native picture offers key benefits, resembling:

  • Instantaneous startup
  • Instantaneous peak efficiency
  • Decreased reminiscence consumption

GraalVM is a high-performance, cloud-native, polyglot runtime that gives vital enhancements in software efficiency and effectivity which is right for microservices. Developed by Oracle Labs, GraalVM goals to enhance the efficiency of JVM-based languages to match the efficiency of native languages. It additionally goals to cut back the startup time of JVM-based functions by compiling them forward of time (AOT) with GraalVM Native Picture expertise.

Native Picture is an revolutionary expertise that compiles Java code forward of time (AOT) right into a standalone binary executable, known as a local picture. The AOT compiler executes a number of duties throughout construct time that reduces startup time resembling static evaluation, removing of unused code, creating fastened classpath, and so on.. The Native Picture builder, native-image, is a utility that processes all lessons of an software and their dependencies, together with these from the JDK. It statically analyzes these information to find out which lessons and strategies are reachable throughout the software execution. Then it ahead-of-time compiles that reachable code and information to a local executable for a particular working system and structure.

The primary downside of the AOT compiler is its lengthy construct time. The AOT engine evaluates circumstances at construct time with the intention to generate an optimized software context and Spring factories (the plugin system behind Spring Boot) particularly crafted for our software. In apply, which means:

  • Much less Spring infrastructure to execute at runtime.
  • Fewer circumstances to judge at runtime.
  • Much less reflection, since programmatic bean registration is used.

To exhibit how we will combine Spring Native right into a Spring Boot microservice, we are going to convert a demo Spring Boot service named customer-service right into a native Spring Boot service, customer-service-native, by following the steps under to expertise the efficiency increase introduced by Spring Native.

Step 1: Set up GraalVM and the native-image Instrument

Seek advice from instructions on getting started with GraalVM to put in each GraalVM and Native Picture. For Home windows customers, there are just a few additional steps to put in Visible Studio with Home windows 10 SDK, make certain to not miss these steps, particulars will be discovered on story using GraalVM and Native Image on Windows 10. On Home windows, the native-image instrument solely works when it’s executed from the Visible Studio’s x64 Native Instruments Command Immediate.

Step 2: Adjustments in pom.xml

The purpose of Spring Native is to help the compilation of present or new Spring Boot functions to native executables. Unchanged. It is a key level! We don’t count on any code change when introducing Spring Native into Spring Boot microservices. Solely construct file adjustments resembling pom.xml, or any configuration change associated to Native Hints (see the part under on Native Hints).

We have to make the next adjustments in pom.xml:

  • add spring-native dependency
  • add spring-aot-maven-plugin
  • add spring repositories for native construct
  • choice to construct native picture or docker picture utilizing Buildpacks
  • guarantee any dependency with runtime scope has its scope eliminated, resembling H2, see under. In any other case, native picture construct will fail, in H2’s case, it complains package deal org.h2.server.internet doesn't exist, which is smart because the native picture is constructed at construct time, dependency marked as runtime scope won’t be seen throughout construct time. The easy repair is to simply take away the scope line for that dependency.

One key level to bear in mind is that AOT compiler does take for much longer throughout construct time (> 8 minutes in constructing our customer-service-native app), which isn’t fascinating throughout the improvement section. To work round it, we will introduce a number of maven profiles:

  • The default profile permits our app to run as a standard Spring Boot app with out spring-native dependency. That is preferrred for builds throughout the improvement section. Builders gained’t even discover that the Spring Boot app they’re engaged on has Spring Native help.
  • The spring-native profile provides spring-native dependency and AOT maven plugin. This bakes Spring Native into our Spring Boot app. Discover we’re including spring-native dependency on this profile solely, in order that the default profile shouldn’t be affected by spring-native.
  • The build-docker-image profile builds the app right into a Docker picture through paketobuildpacks/builder:tiny builder, which requires Docker set up.
  • The build-native-image profile makes use of GraalVM Native Picture to construct the app as a local executable.

Per, the newest Spring Native model 0.11.4, as of this writing, has been examined in opposition to Spring Boot 2.6.6. Let’s check out the whole pom.xml with the adjustments, particularly the totally different profiles, talked about above:

Step 3: Construct Spring Boot native app

As talked about above, there are two methods to construct the Spring Boot native app:

  • Utilizing Spring Boot Buildpacks help to generate a light-weight container containing a local executable, assuming Docker is already put in.
mvn clear package deal spring-boot:build-image -Pspring-native,build-docker-image
  • Utilizing the Native Construct Instruments to generate a local executable.
mvn clear package deal spring-boot:build-image -Pspring-native,build-native-image

Step 4: Run the Spring Boot native app

After a profitable Buildpacks construct with build-docker-image, to launch the Docker container, run the next to launch our demo native app. Discover the --rm flag was added to instruct Docker to routinely clear up the container and take away the file system when the container exits.

docker run --rm wenqi/customerservice:newest

In the event you construct your native app utilizing the Native Construct Instruments build-native-image, an executable file will get generated beneath the goal listing. In our case, the file title is com.github.wenqiglantz.service.customerservice.customerserviceapplication.exe. Simply launch the app by operating the executable:


0.696 seconds! Wonderful!

Let’s put these two demo apps aspect by aspect and examine a few of the key information factors. A load check was additionally carried out through the use of JMeter, monitoring through VisualVM, with 50 customers creating clients and one other 50 customers retrieving buyer information. The load check was carried on for a length of 5 minutes for every app.

  • customer-service: constructed and ran on amazon-corretto-
  • customer-service-native: constructed and ran on graalvm-ce-java17-
  • Exams are carried out on my Dell Latitude 7400, Intel Core i7–8665U CPU @ 1.90GHz, 2112 Mhz, 4 Cores, and eight Logical Processors. 16GB RAM. OS Microsoft Home windows 10 Enterprise, x64-based.
Build time comparison between Spring Boot app and Spring Boot app with Spring Native support
Memory footprint comparison between Spring Boot app and Spring Boot app with Spring Native support
  • Native Spring Boot app has a for much longer construct time, as anticipated attributable to its AOT compiler.
  • Native Spring Boot app has lightning-fast startup time, as designed attributable to its GraalVM and AOT compiler. This is likely one of the the reason why we’re integrating Spring Native into our Spring Boot apps. To attain such wonderful efficiency increase, I can fortunately dwell with the lengthy construct time. 🙂 Higher but, we will use the default maven profile for native improvement builds and hand over the native picture builds to a CI pipeline.
  • Native Spring Boot app has a a lot decrease reminiscence footprint at each startup and beneath load. Heap measurement and used heap for the native app are a lot decrease than these for the non-native app, particularly beneath load, which is sort of spectacular! Proof that native apps are the best way to go!

Spring Native Beta was rolled out in March of 2021. As of probably the most present model 0.11.4, it helps the next frequent Spring Boot starter libraries, discover some libraries do require configuration modification, so it’s not a whole seamless expertise, but:

  • spring-boot-starter-data-jdbc
  • spring-boot-starter-data-jpa (with configuration modification)
  • spring-boot-starter-aop
  • spring-boot-starter-batch
  • spring-boot-starter-web (with configuration modification)
  • spring-boot-starter-actuator (with configuration modification)
  • spring-boot-starter-test (mockito shouldn’t be supported but)
  • and so on.

For an in depth record of which libraries Spring Native helps and which libraries want configuration modification, please confer with Spring’s web site on Spring Boot Support.

Whereas engaged on the demo native app, I bumped into a number of points associated to dependencies together with JPA, OpenAPI, and Liquibase. As of this writing, Spring Native nonetheless doesn’t help Liquibase, there’s an open JIRA ticket for this challenge.

There are fairly just a few libraries that aren’t supported or absolutely supported by Spring Native but, so I needed to look to Native Hints for decision.

As specified on Spring’s Native Hints page, to make use of a characteristic or library not but supported by Spring Native, we will manually add static information beneath META-INF/native-image which can be routinely found by the GraalVM native picture. That is known as “Native Hints”, an fascinating time period the Spring Native workforce coined.

I bumped into lacking PostgreSQLDialect error throughout my native app launch. After explicitly including the next snippet in reflect-config.json beneath listing: META-INFnative-imageorgspringframeworkaotspring-aotreflect-config.json, the error went away. Sure, it is a new listing/file that needs to be added to manually give trace to Spring Native to find the lessons on this file.


"name": "org.hibernate.dialect.PostgreSQLDialect",
"typeReachable": "org.postgresql.Driver"
"allDeclaredConstructors": true,
"allDeclaredMethods": true


I additionally bumped into the next Liquibase error throughout my native app launch:

Native reflection configuration for liquibase.configuration.LiquibaseConfiguration.<init>() is lacking

After including LiquibaseConfiguration class into reflect-config.json, I ran right into a collection of extra errors associated to different Liquibase lessons being lacking. After some analysis, I found Josh Long’s solution to work round Spring Native’s lack of help for Liquibase. A somewhat verbose resolution, it really works nonetheless. Till Spring Native can give you a extra elegant resolution, we must dwell with this workaround for now. Take a look at my GitHub repo for the final version of my reflect-config.json.

As famous in its package deal naming conference, Spring Native remains to be in “experimental” module, which suggests it’s not but absolutely manufacturing prepared. As well as, there are nonetheless some limitations to native pictures:

  • It doesn’t help all Java options
  • Reflection requires a particular configuration
  • Lazy class loading is unavailable

Additionally price mentioning, help for Spring Native presently shouldn’t be as ample as another standard Spring libraries. The group is extra looking out, not fairly able to actively embrace it, but.

The way forward for Spring Native is first-class native help in Spring Boot 3.x! Spring Boot 3 AOT and native help intention for:

  • Seamless integration in Spring Boot
  • Elevated native help
  • Runtime effectivity through AOT transformations for native and JVM
  • Java 17 baseline

Spring Boot 3 common availability launch date is presently scheduled for late November 2022. With a dedication to constructing a robust Java native ecosystem, the Spring workforce guarantees Spring Boot 3 goes to be a framework for the subsequent decade! Thrilling time certainly!

Spring Boot 3 AOT optimizations and native help will profit tens of millions of Spring Boot functions. Whereas we eagerly await this momentous launch, let’s begin utilizing Spring Native to compile our Spring Boot functions into native executables. In the midst of experimenting with Spring Native, I used to be actually hoping to see a extra rising developer base round Spring Native to permit higher collaboration and help for each other, so all of us get to expertise flying excessive on Spring Boot with Spring Native as its wings!

We explored Spring Native on this story by wanting into GraalVM, Native Picture, and AOT compiler. We then walked by the steps in integrating Spring Native into an present Spring Boot app. We additionally checked out a few of the limitations of Spring Native. The way forward for Spring Native and Spring Boot 3 is tremendous thrilling! I hope this story provides you a greater understanding of what Spring Native is, its execs and cons, and its tremendous promising future.

Take a look at my GitHub repo for the demo apps.

Glad coding!

More Posts