DEV Community

Cover image for πŸš€ Building Spring by Example V2: Completing Module 11 - GraalVM Native
Mujuzi Moses ο£Ώ
Mujuzi Moses ο£Ώ

Posted on

πŸš€ Building Spring by Example V2: Completing Module 11 - GraalVM Native

When I started learning Spring, I noticed something about many of the tutorials I found.

They often showed how to use a feature, but not always why it worked or when you should choose one approach over another.

I learn best by building things and documenting what I discover, so I started a project called spring-by-example.

The idea is simple: instead of building one large application, I’m creating a collection of small, focused examples where each example explores a specific Spring concept with clear explanations, tests, and documentation.

I’m happy to say that I’ve now completed Module 11 - GraalVM Native. πŸŽ‰

This module was particularly interesting because it took me beyond the traditional way of running Spring applications.

I explored how Spring applications can be prepared through ahead-of-time processing, compiled into native executables using GraalVM Native Image, and packaged into containers.

The module covers:

  • Native Executables
  • Spring AOT
  • Native Image Constraints
  • Reflection
  • Resource Configuration
  • Runtime Hints
  • Build-Time Processing
  • Native Testing
  • Containerized Native Applications

One of the first things I explored was GraalVM Native Image.

Traditionally, Java applications run on the Java Virtual Machine (JVM), where classes are loaded and compiled at runtime.

GraalVM Native Image takes a different approach by analysing an application ahead of time and compiling it into a native executable.

This changes how the application is built and started, and it introduces some important considerations around reflection, resources, and dynamic class loading.

I then explored Spring AOT (Ahead-of-Time) Processing.

Spring AOT prepares an application at build time by analysing its configuration and generating code that can be used when creating a native image.

This helped me understand that native compilation isn't simply a matter of changing the way an application is packaged. The application also needs to be prepared for the constraints of a native executable.

That led me to Native Image Constraints.

Native Image uses static analysis to determine which classes, methods, and resources are required by an application.

This is useful because it allows the compiler to optimise the resulting executable, but it also means that certain dynamic features need additional configuration.

Reflection was a particularly interesting example.

In a traditional JVM application, reflection can be used to inspect classes, access methods, and create objects dynamically.

However, native image compilation cannot always determine which classes or methods will be accessed through reflection.

I explored this limitation and how reflection metadata can be provided so that the required classes and methods are available in the native executable.

I also explored Resource Configuration.

Applications sometimes depend on resources such as configuration files, templates, and other files available on the classpath.

Since Native Image performs static analysis, resources that are accessed dynamically may need to be explicitly registered.

This helped me understand why an application that works perfectly on the JVM might fail when compiled into a native executable if its required resources aren't included.

Another important topic was Runtime Hints.

Spring provides runtime hints to describe the additional information required by an application when running as a native image.

These hints can cover reflection, resources, serialization, and other runtime requirements.

For example, registering reflection hints allows Spring applications to declare which types and members need to remain accessible at runtime.

This is especially useful when working with frameworks and libraries that rely on dynamic behaviour.

I then moved on to Build-Time Processing.

This example helped me understand how Spring's AOT processing fits into the application build lifecycle and how generated code contributes to preparing an application for native compilation.

It was useful to see that AOT processing is a distinct build step rather than something that happens automatically when the application starts.

After that, I explored Native Testing.

Testing a Spring application on the JVM doesn't necessarily prove that it will behave correctly as a native executable.

Native compilation introduces additional constraints, so it's important to verify the application in the environment in which it is intended to run.

Finally, I completed the module with Containerized Native Applications.

This was probably one of the most rewarding parts of the module because I brought several of the concepts together.

I created a multi-stage Docker build that:

  • Uses Maven and GraalVM Native Image to build the application.
  • Performs Spring AOT processing and native compilation.
  • Copies the resulting native executable into a smaller runtime image.
  • Runs the application inside a Docker container without requiring a JVM at runtime.

The final Docker image was approximately 193 MB.

I also started the container and verified the REST endpoint:

curl -i http://localhost:8098/api/native/container/message
Enter fullscreen mode Exit fullscreen mode

The application returned:

HTTP/1.1 200

Hello! Welcome to the Containerized Native Application.
Enter fullscreen mode Exit fullscreen mode

Getting this example to build successfully was quite an experience.

Native compilation took considerably longer than a regular Maven build, especially on my development machine. I also encountered compatibility issues between GraalVM versions and native-image metadata.

Working through those challenges helped me appreciate that native compilation involves more than simply adding a Maven plugin or changing a Docker base image.

It requires understanding the application's runtime requirements, the build environment, and the constraints imposed by static analysis.

One of my biggest takeaways from this module is that native compilation is not just about producing a smaller or faster executable. It's about understanding how an application behaves when many of the decisions traditionally made at runtime must be resolved ahead of time.

The goal isn't just to learn how to build a native executable.

The bigger goal is to understand what Spring AOT and GraalVM Native Image are doing, why certain application features need additional configuration, and when native deployment makes sense.

The project is open source, and I'm continuing to build it as I learn:

πŸ”— GitHub: https://gith.ub/dsAQrAU5

πŸ”— GitLab: https://gitl.ab/gfDEyGH3

There’s still a long way to go, but completing Module 11 - GraalVM Native feels like another important milestone. πŸš€

One focused example at a time. πŸš€

If you've worked with GraalVM Native Image or Spring AOT before, what has been your biggest challenge when preparing a Spring application for native compilation?

java #springframework #springboot #graalvm #nativeimage #springaot #docker #opensource #github #learninginpublic #backend #softwareengineering

Top comments (0)