Sub topic 13
Using native images with Spring Boot, Quarkus and Micronaut
This chapter assumes you can build a native executable with native-image, and that you know what reachability metadata is and why reflection needs it. It also assumes basic Maven or Gradle skills. Frameworks sit on top of those mechanics. Your job is to see what each one does for you and what is still left to you.
Why frameworks matter for native images
A typical Java framework does a lot of work at run time. It scans the classpath, reads annotations by reflection, creates proxies, and evaluates conditions to decide which beans or components exist. All of this conflicts with the closed-world assumption. The native-image tool must know at build time which classes and members are reachable. Hand-writing reflect-config.json for a whole framework is not realistic.
Spring Boot, Quarkus and Micronaut solve this by moving framework work from run time to build time. Each does it differently. The result is the same in all three. The application's structure is fixed when you build, and the native image receives generated code and generated metadata.
Spring Boot
Spring Boot 3 and later supports native images through Spring AOT. During the build, a process-aot step starts the application context in a special mode. It does not run your application. It inspects the bean definitions and generates Java source for them, plus reachability metadata. The generated code replaces reflection-based bean creation.
If your project inherits from spring-boot-starter-parent, a native profile already exists. It wires in the GraalVM native build plugin and the AOT goals. You can build in two ways:
# Build a native executable on the host (needs GraalVM installed)
mvn -Pnative native:compile
# Build a container image with Cloud Native Buildpacks (needs Docker)
mvn -Pnative spring-boot:build-imageThe second command compiles inside a container, so you do not need GraalVM on your machine. It is also the easiest way to build a Linux executable from macOS or Windows.
What changes in your code
Because the bean graph is fixed at build time, a few Spring features behave differently:
@Profileand@ConditionalOnPropertyare evaluated during AOT processing. The values you use at build time are baked in. Changingspring.profiles.activeat run time does not add or remove beans.- Beans defined by
@Beanmethods are fine. Beans registered dynamically inBeanFactoryPostProcessorcode are not visible to AOT. - Libraries that use reflection internally may need hints.
When you need reflection that Spring cannot infer, register hints with the RuntimeHints API:
package com.example;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportRuntimeHints;
@Configuration
@ImportRuntimeHints(AppConfig.GreeterHints.class)
public class AppConfig {
static class GreeterHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.reflection().registerType(
Greeter.class,
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.INVOKE_PUBLIC_METHODS);
hints.resources().registerPattern("templates/*.txt");
}
}
}This does the same job as the reflect-config.json and resource-config.json entries you wrote by hand in earlier chapters. Spring writes the JSON for you during the build. Spring Boot also offers a nativeTest profile (mvn -PnativeTest test) that runs your tests as a native image. Use it. It finds missing hints before production does.
Quarkus
Quarkus was designed around build-time processing from the start. Its build step, called augmentation, reads your annotations and configuration. It then generates bytecode that wires the application together. At run time, almost no scanning or reflection remains. Native image is a first-class target, not an add-on.
You enable a native build with a property:
# Native build using a local GraalVM or Mandrel
./mvnw package -Dnative
# Native build inside a container, no local GraalVM needed
./mvnw package -Dnative -Dquarkus.native.container-build=trueMandrel is Red Hat's distribution of the Native Image builder, made for Quarkus. Quarkus works with it and with GraalVM.
Extensions carry the metadata. When you add a Quarkus extension such as RESTEasy Reactive or Hibernate ORM, the extension registers the reflection and resource entries it needs. You only handle your own classes that reflection reaches, such as types serialized to JSON by name. For those, use @RegisterForReflection:
package com.example;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public class Greeting {
private String message;
public Greeting() {
}
public Greeting(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}You can pass extra flags to native-image through configuration, for example quarkus.native.additional-build-args=--initialize-at-run-time=com.example.Clock. This links back to the chapter on class initialization. Quarkus already makes sensible choices, and you override them only when a build error tells you to.
Micronaut
Micronaut avoids reflection by using annotation processors at javac time. While your code compiles, Micronaut generates bean definitions, injection points and proxies as ordinary classes. There is no runtime classpath scanning and no runtime proxy generation. This design suits native images, because the generated classes are plainly reachable code.
You build with a packaging property in Maven, or a task in Gradle:
# Maven
./mvnw package -Dpackaging=native-image
# Gradle
./gradlew nativeCompileFor data that needs reflection-like access, such as JSON binding, Micronaut offers introspection. @Introspected generates accessor code at compile time, so Jackson-style serialization works without runtime reflection:
package com.example;
import io.micronaut.core.annotation.Introspected;
@Introspected
public record Greeting(String message) {
}When you do need real reflection, for example for a third-party library, you have options. @ReflectiveAccess marks your own elements. @TypeHint registers classes you cannot annotate. Micronaut writes the result into the metadata that native-image reads.
Comparing the three
| Aspect | Spring Boot | Quarkus | Micronaut |
|---|---|---|---|
| Build-time mechanism | AOT processing of the application context | Augmentation step | Annotation processors in `javac` |
| Native build command (Maven) | `mvn -Pnative native:compile` | `./mvnw package -Dnative` | `./mvnw package -Dpackaging=native-image` |
| Hint for your own classes | `RuntimeHints`, `@RegisterForReflection`-style hints via `@ImportRuntimeHints` | `@RegisterForReflection` | `@Introspected`, `@ReflectiveAccess`, `@TypeHint` |
| Run-time flexibility lost | Profiles and conditions fixed at build time | Config marked build-time is fixed | Bean set fixed at compile time |
| Container build | `spring-boot:build-image` | `-Dquarkus.native.container-build=true` | Docker-based build options |
The commands change between versions, so check the documentation of the version you use.
Build-time configuration traps
The same trap appears in all three frameworks. A setting that the framework reads at build time cannot be changed when the executable starts. Examples include the active profile in Spring, properties marked as build-time fixed in Quarkus, and the set of beans in Micronaut. If you build with dev settings and deploy to production, the executable still behaves like dev for those settings.
Verifying and diagnosing
Follow this loop on any framework project:
- Run the application on the JVM and confirm it works.
- Build the native executable and start it.
- Exercise every code path that uses reflection, resources or serialization. Do this through tests (
nativeTestin Spring,@QuarkusIntegrationTestin Quarkus) instead of only clicking through the application. - If something fails with a
ClassNotFoundException, aNoSuchMethodExceptionor a missing resource, add a hint with the framework mechanism first. - If the failure comes from a library the framework does not know, run the JVM build with the tracing agent and merge the generated metadata, as covered earlier.
Choosing a framework for native work depends on more than native support. All three produce working native executables. Look at your team's existing skills, the libraries you depend on, and how well each library's extension or starter covers its native needs. A library with no native support in your framework is the most common reason a project struggles, not the framework itself.
Exercise
Your Spring Boot application behaves correctly on the JVM with --spring.profiles.active=prod. After you build the native executable with the default profile and start it with the same argument, a bean that only exists in the prod profile is missing. Why does this happen, and what is the fix?
Show answer
Spring AOT evaluates @Profile and conditions during the build, so the bean set is fixed in the generated code. The native executable was built without the prod profile and never contained that bean. The fix is to run AOT processing with the prod profile active (for example by setting spring.profiles.active=prod for the build, or by using the -Dspring.aot.profiles=prod style option your Spring Boot version provides), then rebuild the executable.
Exercise
A Quarkus application serializes a Greeting class to JSON with a library that finds getters by reflection. It works on the JVM but returns an empty object {} in the native executable. What is the likely cause, and which annotation fixes it?
Show answer
The class is not registered for reflection, so the image does not keep its members. Annotate Greeting with @RegisterForReflection. Quarkus then writes the reflection metadata for the class during the build.
Exercise
Name the build-time mechanism each framework uses to avoid runtime reflection: Spring Boot, Quarkus and Micronaut.
Show answer
Spring Boot uses AOT processing of the application context. Quarkus uses its build-time augmentation step. Micronaut uses annotation processors that run during javac compilation.