Sub topic 10
Registering runtime hints in Spring
This chapter assumes you can build a Spring Boot project with Maven or Gradle, you know what reflection and classpath resources are, and you have read the earlier chapters on the closed-world assumption and reachability metadata.
Why hints exist
GraalVM's static analysis decides at build time which classes, methods and resources go into the executable. Code that reaches a class through reflection, or loads a file by name from the classpath, is invisible to that analysis. The build leaves those things out, and the application fails at runtime.
Spring AOT already registers hints for its own features: beans, auto-configuration, @ConfigurationProperties binding and many common libraries. You write hints yourself when your code, or a library without its own metadata, uses reflection, resources or proxies in a way Spring cannot see.
A runtime hint is a declaration that says: keep this class reachable by reflection, or include this resource. Spring turns hints into reachability metadata files that native-image reads during the build.
Writing a RuntimeHintsRegistrar
A RuntimeHintsRegistrar is an interface with one method, registerHints. You receive a RuntimeHints object and add entries for reflection, resources, serialization or proxies.
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) {}
}The pieces work as follows:
@ImportRuntimeHintsattaches the registrar to a configuration class. Spring runs it during AOT processing.reflection().registerType(...)makesGreetingavailable to reflection. TheMemberCategoryvalues say which members: here, public constructors and public methods can be invoked.resources().registerPattern(...)includestemplates/greeting.txtin the executable. Without this,getResourceAsStreamreturnsnullat runtime.
Request only the categories you need. Broader categories keep more code and enlarge the executable.
Other ways to register hints
| Approach | Use it when |
|---|---|
| `@ImportRuntimeHints` | You want hints tied to a specific configuration or component. |
| `@RegisterReflectionForBinding` | A class is only used for data binding, such as JSON serialization, and needs reflection access. |
| `META-INF/spring/aot.factories` | A library should contribute hints to every application that uses it. |
| Static JSON under `META-INF/native-image` | You already have metadata files, for example from the tracing agent. |
Checking your hints
Hints can only be verified in native mode. A JVM run never fails on missing metadata. Spring also offers RuntimeHintsPredicates in tests, which check that a registrar produced the expected hint without a native build:
import static org.assertj.core.api.Assertions.assertThat;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.junit.jupiter.api.Test;
class GreetingHintsTests {
@Test
void registersGreetingHints() {
RuntimeHints hints = new RuntimeHints();
new AppConfig.GreetingHints().registerHints(hints, getClass().getClassLoader());
assertThat(RuntimeHintsPredicates.reflection()
.onType(AppConfig.Greeting.class)).accepts(hints);
assertThat(RuntimeHintsPredicates.resource()
.forResource("templates/greeting.txt")).accepts(hints);
}
}This test must live in the same package as AppConfig, because GreetingHints is package-private.
Common errors
- Missing resource at runtime. A
nullstream orFileNotFoundExceptionfor a classpath file means no resource hint covers its path. NoSuchMethodExceptionorClassNotFoundExceptionfor your own class. The class was never registered for reflection, or theMemberCategoryis too narrow for the member you access.- Hints seem ignored. The registrar is not imported by any configuration class that Spring processes, or it is registered for a different class than the one used at runtime.
Exercise
Your native executable throws a NullPointerException when it reads templates/greeting.txt with getResourceAsStream, though the same code works on the JVM. What do you add, and why does the JVM run work without it?
Show answer
Add hints.resources().registerPattern("templates/greeting.txt") in a RuntimeHintsRegistrar and attach it with @ImportRuntimeHints. The JVM can read any file on the classpath at runtime. The native build only includes resources it was told about, so the unregistered file is missing and getResourceAsStream returns null.