Sub topic 3
Registering Runtime Hints in Spring
Assume Java 17 or later, Spring Boot 3, basic Spring beans, and Maven or Gradle experience. You should also know that reflection discovers classes or methods at runtime.
Registering runtime hints in Spring
A native image uses closed-world analysis. It includes code that the build can prove is reachable. Reflection, classpath resources, and dynamic proxies can hide that usage. Spring runtime hints provide this missing information during ahead-of-time (AOT) processing.
Implement RuntimeHintsRegistrar and attach it with @ImportRuntimeHints:
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.GreetingHints.class)
public class AppConfig {
static class GreetingHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.reflection().registerType(
Greeting.class,
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS,
MemberCategory.INVOKE_PUBLIC_METHODS);
hints.resources().registerPattern("templates/greeting.txt");
}
}
public record Greeting(String text) {}
}Register only the access that the application needs. Extra metadata increases the executable and can hide configuration mistakes.
Collecting metadata with the tracing agent
The GraalVM tracing agent observes a JVM run and writes reachability metadata. Exercise important application paths while it runs:
java -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image \
-jar target/demo.jarTreat the generated files as a starting point. Review them, remove accidental entries, and repeat the run when application behavior changes.
Running tests as a native image
Native testing catches failures that ordinary JVM tests miss. With the Spring Boot Maven native-test profile, run:
./mvnw -PnativeTest testDiagnosing native image failures
Check the first error, then identify the missing category:
| Symptom | Likely cause |
|---|---|
| Class or constructor not found | Reflection hint is missing |
| Resource not found | Resource pattern is missing |
| Proxy creation fails | Proxy metadata is missing |
| Bean differs from JVM run | A condition was evaluated during AOT |
Also verify that build-time properties and profiles match the intended deployment.
Choosing native images
| Benefit | Cost |
|---|---|
| Fast startup and lower memory use | Longer, more resource-intensive builds |
| Useful for short-lived services | Dynamic behavior needs metadata |
| Small runtime without a JVM | Some libraries need native support |
Native images suit serverless handlers and rapidly scaling services. A JVM remains simpler for long-running applications that benefit from JIT optimization or extensive runtime dynamism.
Exercise
Why might a feature bean exist on the JVM but be absent from a native executable?
Show answer
Its condition may have been evaluated during AOT processing with a different build-time property value.