Sub topic 13
Using native images with Spring Boot, Quarkus and Micronaut
This chapter assumes you know how reachability metadata works, how the Maven and Gradle native plugins are configured, and why the closed-world assumption matters. Writing that metadata by hand for a whole framework is impractical. Spring Boot, Quarkus and Micronaut each do most of it for you.
What the frameworks do for you
A typical JVM web application scans the classpath, reads annotations through reflection, and creates proxies at startup. All of that is dynamic behavior that static analysis cannot see. The three frameworks respond in the same way: they move that work from run time to build time.
| Framework | Build-time mechanism | Typical native build command |
|---|---|---|
| Spring Boot 3 | Spring AOT processing generates bean definitions and runtime hints | `mvn -Pnative native:compile` |
| Quarkus | Build steps in the extension system produce metadata and bytecode | `./mvnw package -Dnative` |
| Micronaut | Annotation processors generate bean definitions at compile time | `./mvnw package -Dpackaging=native-image` |
Spring Boot's parent POM already defines a native profile that wires in the GraalVM plugin you saw earlier. Micronaut's Gradle projects use ./gradlew nativeCompile. Quarkus can also build inside a container with -Dquarkus.native.container-build=true, so you do not need a local GraalVM installation.
Where the frameworks need your help
The frameworks cover their own code and the libraries they integrate with. They cannot see how your own code uses reflection. The usual case is a class that a JSON library creates or reads by reflection, such as a request or response type. Each framework has an annotation for this.
Spring Boot registers a type for data binding like this:
import org.springframework.aot.hint.annotation.RegisterReflectionForBinding;
import org.springframework.context.annotation.Configuration;
record GreetingDto(String text) {}
@Configuration
@RegisterReflectionForBinding(GreetingDto.class)
public class NativeHints {
}Quarkus uses @RegisterForReflection on the class itself:
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public class Greeting {
public String text;
}Micronaut avoids reflection by generating introspection data at compile time:
import io.micronaut.core.annotation.Introspected;
@Introspected
public record Greeting(String text) {
}For anything the annotations do not cover, you can still put hand-written reflect-config.json files under META-INF/native-image/. Spring also offers RuntimeHintsRegistrar with @ImportRuntimeHints for programmatic hints.
Consequences of build-time processing
Moving work to build time changes how these applications behave.
- The configuration that decides the bean graph is fixed at build time. In Spring Boot,
@Profileand@ConditionalOnPropertyconditions are evaluated during AOT processing. Changing the active profile at run time does not add beans that were excluded from the image. Quarkus has a similar split between build-time properties and run-time properties. - Build time grows. Expect the framework build step plus the
native-imagestep. Many teams run most tests on the JVM and build the native image only in CI. - Libraries differ in support. A library that relies on runtime bytecode generation or heavy reflection may not work, or may need metadata. Check the framework's list of supported libraries. The GraalVM Reachability Metadata Repository supplies metadata for many popular libraries, and the Maven and Gradle plugins can use it.
- Native tests matter. A test that passes on the JVM can still fail in the image because of missing metadata. Run the application's tests against the native build at least once before release. Spring and the GraalVM plugins provide a
nativeTestgoal for this.
Choosing between them
All three produce comparable executables. The choice usually depends on the framework you already use. Spring Boot has the largest ecosystem but depends more on the AOT step to handle its dynamic design. Quarkus and Micronaut were designed around build-time processing, so less of their code needed adapting. If an existing application fails, use the tracing agent from earlier to find the missing metadata. Then move the fix into a framework annotation or hint.
Exercise
A Spring Boot application works on the JVM. After building a native image, a REST endpoint fails because a JSON response class cannot be serialized. Which fix matches the framework's approach, and why is it needed?
Show answer
Register the class with @RegisterReflectionForBinding (or a RuntimeHintsRegistrar). The JSON library reads the class's fields and constructors by reflection, and static analysis cannot see that use. Without the hint, the metadata is missing from the image.
Exercise
You deploy a Spring Boot native image and start it with a different spring.profiles.active value than the one used at build time. Beans that belong only to the new profile are missing. Why?
Show answer
Spring AOT evaluates profile and conditional bean definitions during the build. The image contains only the bean graph produced for the build-time configuration, so the run-time profile cannot add beans that were excluded.