Moritz Halbritter @m_halbritter
Codineers Meetup Rosenheim
2022-10-20
GraalVM Native
and Spring Boot 3.0
Copyright © 2022 VMware, Inc. or its affiliates.
Cover w/ Image
● Why GraalVM Native support?
● Support strategies for Frameworks
● AOT processing in Spring
● Will this work with my app?
● Developer cookbook
Agenda
Why GraalVM Native
support in Spring?
Why consider GraalVM Native as an option?
Runtime characteristics
● Memory efficiency (from GB to MB)
● Startup time (from s to ms)
● Predictable startup performance
Production considerations
● Competing with other languages (Golang, Node.JS)
● Well suited for CLIs, functions, scale to zero
● Container density on your platform
● Runtime reflection predictability (think CVEs)
GraalVM challenges
Hints needed for
● Reflection (dynamic class loading, querying, invoking)
● Resource loading
● Proxies
● Serialization
● JNI
Spring Native experimental project
A short history of Spring Native
● Incubation started in 2019
● Several generations, trying different approaches
● Lots of community feedback
● Spring Native and GraalVM evolved at the same time, goals align well
● First class support in Spring Framework 6.0 and Spring Boot 3.0
● Spring Native to be officially retired
Support strategies for
Frameworks
Building a native image with GraalVM
Process all classes
in a fixed classpath
Using the “native-image" binary
Perform
static analysis
Compile code + data
into a single binary
+
Reachability
Metadata
How Spring adapts for native
Perform
static analysis
Compile code + data
into a single binary
Infer metadata if
needed
Generate optimized
version of the
application context
Process all classes
in a fixed classpath
Reachability
Metadata
+
Also applies to resources, JDK proxies… See class metadata features
AOT processing in Spring
AOT processing in Spring Boot 3.0
App compiled
Classes
Resources
AOT is done
Classes
Resources
AOT code
(source + classes)
AOT metadata
(generated)
Native binary
Native application
:processAot
+
:aotClasses
:nativeCompile
Application Jar
AOT optimized app
:bootJar
Libraries metadata
(reachability repo)
Native binary
Optimized Container
:bootBuildImage
(BuildPacks)
AOT phase in Spring
Generating functional configuration
● Skips the @Configuration model at runtime
● Generate available, debuggable source code
● Perfect fit with GraalVM native image static analysis
● Reachability metadata generated as needed
Generating AOT sources
Context refresh
for AOT
Generate source code
+ hints
Running this code will
re-create the same
state at runtime
Detect classpath,
environment, and
main class
Only bean definitions
are created,
no bean instances!
> Demo
AOT processing
https://github.com/mhalbritter/spring-aot-demo-application
Will this work with my app?
“Closed world” assumptions
Runtime flexibility constraints
● Application Classpath is fixed at build time
● Spring Profile is fixed at build time
● Environment changes impacting the context are not supported
(think, “spring.some.feature.enabled=true”)
Native image constraints
● Java agents are not supported
● Reading/manipulating bytecode: please don’t
3rd party libraries support
GraalVM reachability metadata repository
● Community can contribute to this shared repo
● All 3rd party library metadata written by the Spring team is contributed there
● Collaboration with other Frameworks to migrate their own
Long term goals
● Ideally, libraries should ship metadata directly
● Work on baking this feature into the code directly
Testing your Native application
JVM test strategies
● Running your app in AOT mode on the JVM
● Use Spring’s testing utilities for advanced cases
(RuntimeHintsAgent, RuntimeHintsPredicates)
Executing your existing test suite as Native
● JUnit and Spring Test context support available, compile and run your test suite
● New Native debugging support in IntelliJ IDEA
Developer cookbook
Recipe: compile an existing app to Native
1. Execute your app in AOT mode on the JVM
2. Run existing test suite in native
3. Contribute library hints to the community repository
4. Test your hints with the RuntimeHintsAgent
5. If you generated code (DON’T), test it with the TestCompiler
Spring apps adoption 🍃
Being part of the GraalVM
community 🤗
New developers and use cases 🆕
Thank you
The State of Spring
https://spring.io/survey
© 2022 Spring. A VMware-backed project.
Cover w/ Image
Revolutionizing Java-Based
Applications with GraalVM
Recording from Devoxx 2022
Hope you didn’t miss it!

GraalVM Native and Spring Boot 3.0

  • 1.
    Moritz Halbritter @m_halbritter CodineersMeetup Rosenheim 2022-10-20 GraalVM Native and Spring Boot 3.0 Copyright © 2022 VMware, Inc. or its affiliates.
  • 2.
    Cover w/ Image ●Why GraalVM Native support? ● Support strategies for Frameworks ● AOT processing in Spring ● Will this work with my app? ● Developer cookbook Agenda
  • 3.
  • 5.
    Why consider GraalVMNative as an option? Runtime characteristics ● Memory efficiency (from GB to MB) ● Startup time (from s to ms) ● Predictable startup performance Production considerations ● Competing with other languages (Golang, Node.JS) ● Well suited for CLIs, functions, scale to zero ● Container density on your platform ● Runtime reflection predictability (think CVEs)
  • 7.
    GraalVM challenges Hints neededfor ● Reflection (dynamic class loading, querying, invoking) ● Resource loading ● Proxies ● Serialization ● JNI
  • 8.
    Spring Native experimentalproject A short history of Spring Native ● Incubation started in 2019 ● Several generations, trying different approaches ● Lots of community feedback ● Spring Native and GraalVM evolved at the same time, goals align well ● First class support in Spring Framework 6.0 and Spring Boot 3.0 ● Spring Native to be officially retired
  • 9.
  • 10.
    Building a nativeimage with GraalVM Process all classes in a fixed classpath Using the “native-image" binary Perform static analysis Compile code + data into a single binary + Reachability Metadata
  • 11.
    How Spring adaptsfor native Perform static analysis Compile code + data into a single binary Infer metadata if needed Generate optimized version of the application context Process all classes in a fixed classpath Reachability Metadata + Also applies to resources, JDK proxies… See class metadata features
  • 12.
  • 13.
    AOT processing inSpring Boot 3.0 App compiled Classes Resources AOT is done Classes Resources AOT code (source + classes) AOT metadata (generated) Native binary Native application :processAot + :aotClasses :nativeCompile Application Jar AOT optimized app :bootJar Libraries metadata (reachability repo) Native binary Optimized Container :bootBuildImage (BuildPacks)
  • 14.
    AOT phase inSpring Generating functional configuration ● Skips the @Configuration model at runtime ● Generate available, debuggable source code ● Perfect fit with GraalVM native image static analysis ● Reachability metadata generated as needed
  • 15.
    Generating AOT sources Contextrefresh for AOT Generate source code + hints Running this code will re-create the same state at runtime Detect classpath, environment, and main class Only bean definitions are created, no bean instances!
  • 16.
  • 17.
    Will this workwith my app?
  • 18.
    “Closed world” assumptions Runtimeflexibility constraints ● Application Classpath is fixed at build time ● Spring Profile is fixed at build time ● Environment changes impacting the context are not supported (think, “spring.some.feature.enabled=true”) Native image constraints ● Java agents are not supported ● Reading/manipulating bytecode: please don’t
  • 19.
    3rd party librariessupport GraalVM reachability metadata repository ● Community can contribute to this shared repo ● All 3rd party library metadata written by the Spring team is contributed there ● Collaboration with other Frameworks to migrate their own Long term goals ● Ideally, libraries should ship metadata directly ● Work on baking this feature into the code directly
  • 20.
    Testing your Nativeapplication JVM test strategies ● Running your app in AOT mode on the JVM ● Use Spring’s testing utilities for advanced cases (RuntimeHintsAgent, RuntimeHintsPredicates) Executing your existing test suite as Native ● JUnit and Spring Test context support available, compile and run your test suite ● New Native debugging support in IntelliJ IDEA
  • 21.
  • 22.
    Recipe: compile anexisting app to Native 1. Execute your app in AOT mode on the JVM 2. Run existing test suite in native 3. Contribute library hints to the community repository 4. Test your hints with the RuntimeHintsAgent 5. If you generated code (DON’T), test it with the TestCompiler
  • 24.
    Spring apps adoption🍃 Being part of the GraalVM community 🤗 New developers and use cases 🆕
  • 25.
    Thank you The Stateof Spring https://spring.io/survey © 2022 Spring. A VMware-backed project.
  • 26.
    Cover w/ Image RevolutionizingJava-Based Applications with GraalVM Recording from Devoxx 2022 Hope you didn’t miss it!