|Compile Spring Applications To Native Images With Spring Native|
|Written by Nikos Vaggalis|
|Tuesday, 23 March 2021|
Spring Native Beta is now available bringing a new way to deploy Spring applications. Spring Native lets you compile Spring applications to native images using the GraalVM native-image compiler.
What's the advantage in that? Instant startup, instant peak performance, and reduced memory consumption, since the native Spring applications are deployed as a standalone executable, well docker image, without including a JVM installation.
What's the disadvantage? It's that the GraalVM build process, which tries to make the most optimal image possible, throws a lot of stuff out. This could be dependencies, resources or parts of your code.
The way to mitigate this is by feeding GraalVM a bunch of configurations files that will cause it to include the extra material in the image.To automate this process you annotate your code with hints in order to direct the Spring AOT plugin to generate those configurations files.
However, the Spring Native bundle includes spring-native-configuration which, in Spring Boot's true spirit, handles it for you. It contains configuration hints for Spring's classes therefore it takes care of the framework without further ado. Some drivers have already been annotated, for instance the MySQL driver supports Spring Native with hints that allow generation of the right entries in native image files, reflect-config.json, resource-config.json and native-image.properties
The Spring AOT build plugin is available for bot Maven and Gradle and is invoked before running your application and tests, and may potentially require additional IDE configuration. The generated files sources are available and can be inspected if there's the need to.
That need arises in the following are situations where
In those circumstances, you can annotate the classes already annotated with @Configuration or @SpringBootApplication with @NativeHint, or in simple enough cases using @TypeHint directly (a @NativeHint is a container for many kinds of configuration including @TypeHints).
This means that in most of the cases, your Spring boot app, JPA and Spring Security included, will compile to native just fine, except if you have some fancy dependencies which you'll need to include yourself by hints or tweaking the generated configuration files.
Besides JPA and Spring Security , Spring Native also works with Spring Cloud so that you can compile a Spring Cloud Function to an image and then deploy it as is on Google Cloud Run, or as a AWS Lambda!
Although in Beta, Spring Native is still experimental which means that you must use it sparingly and not on production code. Still bootstraping a new application on start.spring.io includes a build option for Native already. And on the roadmap there's a lot to look forward to.
or email your comment to: firstname.lastname@example.org
|Last Updated ( Tuesday, 23 March 2021 )|